OKdevTV Docs
Home OKdevTV
ESC
jest 1 min read 1.3 KB Raw Markdown
On this page (9)

Jest

jest 설치

npm i -g jest

파일 테스트

  • 개별 파일
text
jest --watch test/DateUtils.test.js
  • 전체 파일
text
jest --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
js
const config = {
  testEnvironment: 'node'
}
module.exports = config;

Coverage

text
jest --coverage

Timeout

ESM module

  • install babel
sh
npm i -D jest @babel/core @babel/preset-env
  • babel.config.json
json
{
  "presets": ["@babel/preset-env"]
}
  • test file
  • add.js
js
export default function add(a, b) {
  return a + b;
}
  • add.test.js
js
import add from './add';

test('adds 1 + 2 to equal 3', () => {
  expect(add(1, 2)).toBe(3);
})

ref