2024.03.25(월)
jest.mock('./moduleName')
로 명시적으로 호출해주어야 함 (mapping도 가능)__mocks__
폴더에 작성node_modules
와 인접한 __mocks__
폴더에 동일한 module 명으로 mock 모듈 파일을 작성하면 됨jest.mock('./moduleName')
을 호출할 필요 ❌.
├── config
├── __mocks__
│ └── fs.js
├── models
│ ├── __mocks__
│ │ └── user.js
│ └── user.js
├── node_modules
└── views
npm i -D jest ts-jest @types/jest supertest @types/supertest
npx ts-jest config:init
jest.config.js
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
tsconfig.json
…,
"exclude": [
"src/**/*.test.ts",
"src/**/__mocks__/*.ts"
]
}
npm test
Makefile
(반드시 탭(Tab) 사용)all:
cat ./Makefile
test:
npm test
make all
: 현재 Makefile의 내용을 출력make test
: 프로젝트의 테스트를 실행 (전제조건: 프로젝트가 Node.js 및 npm을 사용하여 관리되고 있어야 함)"jest": {
"moduleNameMapper": {
"^@/(.+)": "<rootDir>/src/$1"
}
}
npm test
CI=true npm test
Makefile
(반드시 탭(Tab) 사용)all:
cat ./Makefile
test:
CI=true npm test
make all
: 현재 Makefile의 내용을 출력make test
: 프로젝트의 테스트를 CI 모드로 실행