(Jest, Supertest) API 단위 통합 테스트 - connect-foundation/2019-12 GitHub Wiki
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);
});
});