Jest
- https://jestjs.io/
- 테스트 파일 확장자는 *.test.js 또는 *.spec.js
jest 설치
npm i -g jest
파일 테스트
- 개별 파일
textjest --watch test/DateUtils.test.js
- 전체 파일
textjest --watchAll test/
Sample
js// sum.js
function sum(a, b) {
return a + b;
}
module.exports = sum;
js// sum.test.js
const sum = require('./sum');
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
Function
- toBe
- toBeCloseTo
- toEqual
- toStrictEqual
- toHaveProperty
- toMatchSnapshot
- toThrowError
test ajax
jest.config.js
jsconst config = {
testEnvironment: 'node'
}
module.exports = config;
Coverage
textjest --coverage
Timeout
test('title', () => {}, timeout);- https://exerror.com/timeout-async-callback-was-not-invoked-within-the-5000-ms-timeout-specified-by-jest-settimeout/
ESM module
- install babel
shnpm i -D jest @babel/core @babel/preset-env
babel.config.json
json{
"presets": ["@babel/preset-env"]
}
- test file
- add.js
jsexport default function add(a, b) {
return a + b;
}
- add.test.js
jsimport add from './add';
test('adds 1 + 2 to equal 3', () => {
expect(add(1, 2)).toBe(3);
})