Jest
Jest
Jest는 JavaScript Framework입니다.
🔎 Mocha와 비교To Do …
환경설정
• 설치
npm install --save-dev jest
또는 yarn add --dev jest
를 통해 설치합니다.
• 실행
전역으로 설치했다면 jest [test file]
, 패키지에 설치했다면 package.json script의 test를 수정하여
npm run test
로 실행합니다.
• 설정
- jest.config.js
testEnvironment: "node",
setupFilesAfterEnv: ["./jest.setup.js"],
transform: {
'^.+\\.js?$': 'babel-jest',
'^.+\\.ts?$': 'ts-jest'
}
- testEnvironment로 test 환경을 정의합니다.
- setupfilesAferEnv로 모든 test file의 실행 환경을 설정할 수 있습니다.
- moduleFileExtensions로 module 파일을 정의할 수 있습니다.
- testMatch로 test할 파일들을 정의합니다.
- transfrom으로 nodejs가 읽을 수 있는 형태로 Complie/Transpile 설정을 정의할 수 있습니다.
- transformIgnorePattern로 Complie/Transpile 하지 않을 파일들을 정의할 수 있습니다.
jest-extended를 사용한다면, setupFilesAfterEnv: [”./jest.setup.js”, ‘jest-extended’]로 설정합니다.
Test 함수
• describe
test suite를 정의합니다.
• test (it)
test case를 정의합니다.
describe("test suite", () => {
test("test case", () => {
// To Do
});
it("test case", () => {
// To Do
});
})
🔎 x, skip, only를 통해 테스트 실행 여부를 지정할 수 있습니다.
비교 함수
• 공통
- toEqual : 참조 자료형(객체, 배열)을 비교합니다.
- toBe : 원시 자료형을 비교합니다.
• 참/거짓
- toBeNull : Null인지 비교합니다.
toBeTruthy: Truthy인지 비교합니다.
toBeFalsy: Falsy인지 비교합니다.
• 숫자
- toBeGreaterThan : 기준보다 초과인지 비교합니다.
- toBeLessThan : 기준보다 미만인지 비교합니다.
• 문자열
- toContain : 해당 문자열을 포함하는지 비교합니다.
Snapshot Test
테스트 결과를 스냅샷으로 찍어두고, 이를 기준으로 현재 결과를 비교를 함으로써 테스트함
https://jestjs.io/docs/snapshot-testing
- toMatchSnapshot
스냅샷이 없으면 생성하고, 있으면 스냅샷을 비교함
🔎 스냅샷은 같은 경로 __snapshots__
폴더에 파일이름 .snap
파일을 만듬
https://jestjs.io/docs/expect#tomatchsnapshotpropertymatchers-hint
Mock Test
• jest.fn
jest.fn은 Mock Function을 만듭니다. Mock 함수는 아래 API들을 통해 함수를 모사할 수 있습니다.
- mockReturnValue : mock function의 반환값을 정의합니다.
- mockRejectedValue : reject하는 async 함수를 정의합니다.
- mockResolvedValue: resolved하는 async 함수를 정의합니다.
- mockReset: mockClear에 추가적으로 mock function의 return, implemation을 제거합니다.
- mockClear: mock function의 calls, instances를 제거합니다.
Expect Test
expect를 통해 assertion test를 할 수 있습니다.
• 함수
- toHaveBeenCalled : mock function이 호출되었는지 테스트 할 수 있습니다.
- toHaveBeenCalledWith : mock function이 특정 인자와 함께 호출되었는지 테스트 할 수 있습니다.
- toHaveBeenCleedTimes : function이 몇번 호출되었는지 테스트 할 수 있습니다.
- toHaveReturnedTimes : function이 몇번 반환하는지 테스트 할 수 있습니다.
- toHaveReturnedWith : function이 어떤 값을 반환하는지 테스트할 수 있습니다.
• 객체
- toStrictEqual : 객체(내용)가 같은지 테스트 할 수 있습니다.
- toHaveProperty : 객체가 해당 property를 가지고 있는지 테스트할 수 있습니다.
참고자료
https://codewithhugo.com/jest-array-object-match-contain/ https://www.taniarascia.com/integration-testing-with-jest-typescript-objection/
https://medium.com/@ismailkuruca/how-to-effectively-write-integration-tests-with-nodejs-knex-postgres-bcaa60022080
https://jestjs.io/docs/cli#–maxworkersnumstring https://stackoverflow.com/questions/54422849/jest-testing-multiple-test-file-port-3000-already-in-use
https://medium.com/geoblinktech/postgres-and-integration-testing-in-node-js-apps-2e1b52af7ffc
rejects https://jestjs.io/docs/tutorial-async#rejects https://stackoverflow.com/questions/47144187/can-you-write-async-tests-that-expect-tothrow
middleware unit test https://javascript.plainenglish.io/how-to-unit-test-express-middleware-typescript-jest-c6a7ad166e74
jest module mocking https://www.daleseo.com/jest-mock-modules/
https://stackoverflow.com/questions/64844580/jest-mocking-typeerror-axios-get-mockresolvedvalue-is-not-a-function
stash https://helloinyong.tistory.com/202