Getting Started (ok)

https://jestjs.io/docs/getting-started

Install Jest using yarn:

yarn add --dev jest

Hãy bắt đầu bằng cách viết một bài kiểm tra cho một hàm giả định cộng hai số. Đầu tiên, tạo tệp sum.js:

function sum(a, b) {
  return a + b;
}
module.exports = sum;

Sau đó, tạo một tệp có tên sum.test.js. Điều này sẽ chứa thử nghiệm thực tế của chúng tôi:

const sum = require('./sum');

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

Add the following section to your package.json

{
  "scripts": {
    "test": "jest"
  }
}

Finally, run yarn test or npm run test and Jest will print this message:

Running from command line

Bạn có thể chạy Jest trực tiếp từ CLI (nếu nó có sẵn trên toàn cầu trong PATH của bạn, ví dụ: bằng fiber global add jest hoặc npm install jest --global) với nhiều tùy chọn hữu ích.

Run all tests (default)

jest

Run all sum.test.js

jest sum.test or jest sum.test.js

If you'd like to learn more about running jest through the command line, take a look at the Jest CLI Options page.

Generate a basic configuration file

Based on your project, Jest will ask you a few questions and will create a basic configuration file with a short description for each option:

jest --init

Using Babel

To use Babel, install required dependencies via yarn:

yarn add --dev babel-jest @babel/core @babel/preset-env

Configure Babel to target your current version of Node by creating a babel.config.js file in the root of your project:

// babel.config.js
module.exports = {
  presets: [['@babel/preset-env', {targets: {node: 'current'}}]],
};

Last updated