Skip to content

(Jest, Supertest) API 단위 통합 테스트

Sungdong Jo edited this page Nov 13, 2019 · 2 revisions

Middleware with Jest

import httpMocks from "node-mocks-http";
import status from "http-status";
import { findHouse } from "../../../middleware/house";

describe("Middleware - House", () => {
    it("[체크인/체크아웃, 인원, 가격] 필터로 조회 후 결과를 반환한다.", async () => {
        // given
        const query = {
            checkInOut: [
                new Date("2016-10-01 00:00:00 GMT+0900"),
                new Date("2016-10-03 00:00:00 GMT+0900")
            ],
            capacity: [3],
            price: [0, 100]
        };

        const req = httpMocks.createRequest({
            query
        });
        const res = httpMocks.createResponse();
        const next = jest.fn();

        // when
        await findHouse(req, res, next);

        // then
        expect(res.get("Content-Type")).toEqual("application/json");
        expect(res.json()).toEqual(/* 기대하는 결과 값 */);
        expect(res.statusCode).toEqual(status.OK);
    });
});

Route with Supertest

import supertest from "supertest";
import status from "http-status";
const agent = supertest.agent("http://localhost");

describe("Routes - House", () => {
    test("GET /house", async () => {
        // given
        const query = {
            checkInOut: [
                new Date("2016-10-01 00:00:00 GMT+0900"),
                new Date("2016-10-03 00:00:00 GMT+0900")
            ],
            price: [0, 800],
            capacity: [3]
        };

        // when
        const response = await agent.get("/house").query(query);

        // then
        expect(response.header["content-type"]).toEqual(
            "application/json; charset=utf-8"
        );
        expect(response.body.length > 0).toBeTruthy();
        expect(response.statusCode).toEqual(status.OK);
    });
});

BookUs!

개요
기획서

Tech

실용적인 리액트 테스트 전략
DevOps
Infra Structure
컴포넌트 작성법
Client Sturcture

Documents

데이터베이스 스키마
Yarn workspace 명령어
Docker를 이용한 서버 개발 환경
Linting Tools

Stress Testing Log

테스트 로그

1차 테스트

📝 Agile Process

스프린트 0주차: 기획 단계
스프린트 1주차: 개발 환경 구축
스프린트 2주차: 개발
스프린트 3주차: 개발
스프린트 4주차: 개발
스프린트 5주차: 개발
👉 스프린트 6주차 🔥

👷‍♂️ Technique Writing

🤝 Rules

Clone this wiki locally