> For the complete documentation index, see [llms.txt](https://javascriptuse.gitbook.io/javascript/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://javascriptuse.gitbook.io/javascript/getting-started.md).

# Getting Started (ok)

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

&#x20;Install Jest using [`yarn`](https://yarnpkg.com/en/package/jest):

```
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);
});
```

&#x20;Add the following section to your ***`package.json`***

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

&#x20;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
```

&#x20;If you'd like to learn more about running `jest` through the command line, take a look at the [Jest CLI Options](https://jestjs.io/docs/cli) 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

&#x20;To use [Babel](https://babeljs.io/), install required dependencies via `yarn`:

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

&#x20;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'}}]],
};
```
