Getting Started

使用 yarn 安装 Jest︰

  1. yarn add --dev jest

npm

  1. npm install --save-dev jest

注:Jest的文档统一使用yarn命令,不过使用npm也是可行的。 你可以在yarn的说明文档里看到yarnnpm之间的对比。

让我们开始为一个假设函数编写测试,该函数将两个数字相加。 首先,创建一个 sum.js 文件:

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

然后,创建一个名为 sum.test.js 的文件。 这将包含我们的实际测试:

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

将下面的配置部分添加到你的 package.json 里面:

  1. {
  2. "scripts": {
  3. "test": "jest"
  4. }
  5. }

最后,运行 yarn testnpm run test ,Jest将打印下面这个消息:

  1. PASS ./sum.test.js
  2. adds 1 + 2 to equal 3 (5ms)

你刚刚成功地写了第一个Jest测试 !

此测试使用 expecttoBe 来测试两个值完全相同。 若要了解Jest关于测试方面更多的能力,请参阅 Using Matchers

在命令行运行

你可以通过命令行直接运行Jest(前提是jest已经处于你的环境变量 PATH中,例如通过 yarn global add jestnpm install jest --global安装的Jest) ,并为其指定各种有用的配置项。

这里演示了如何对能匹配到 my-test 的文件运行 Jest、使用config.json 作为一个配置文件、并在运行完成后显示一个原生的操作系统通知。

  1. jest my-test --notify --config=config.json

如果你愿意了解更多关于通过命令行运行 jest 的内容,请继续阅读 Jest CLI 选项 页面。

更多配置

生成一个基础配置文件

基于您的项目,Jest将向您提出几个问题,并将创建一个基本的配置文件,每个选项都有一个简短的说明:

  1. jest --init

使用 Babel

如果需要使用 Babel,可以通过 yarn来安装所需的依赖。

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

可以在工程的根目录下创建一个babel.config.js文件用于配置与你当前Node版本兼容的Babel:

  1. // babel.config.js
  2. module.exports = {
  3. presets: [
  4. [
  5. '@babel/preset-env',
  6. {
  7. targets: {
  8. node: 'current',
  9. },
  10. },
  11. ],
  12. ],
  13. };

Babel的配置取决于具体的项目使用场景 ,可以查阅 Babel官方文档来获取更多详细的信息。

Making your Babel config jest-aware

Jest will set process.env.NODE_ENV to 'test' if it’s not set to something else. You can use that in your configuration to conditionally setup only the compilation needed for Jest, e.g.

  1. // babel.config.js
  2. module.exports = api => {
  3. const isTest = api.env('test');
  4. // You can use isTest to determine what presets and plugins to use.
  5. return {
  6. // ...
  7. };
  8. };

注意:当你安装 Jest 时,babel-jest 是会被自动安装的,并且如果你的项目下存在一个 Babel 配置文件时,它将会自动对相关文件进行转义。 如果要避免这个行为,你可以显式的重置 transform 配置项:

  1. // jest.config.js
  2. module.exports = {
  3. transform: {},
  4. };

Babel 6 支持

Jest 24 dropped support for Babel 6. We highly recommend you to upgrade to Babel 7, which is actively maintained. However, if you cannot upgrade to Babel 7, either keep using Jest 23 or upgrade to Jest 24 with babel-jest locked at version 23, like in the example below:

  1. "dependencies": {
  2. "babel-core": "^6.26.3",
  3. "babel-jest": "^23.6.0",
  4. "babel-preset-env": "^1.7.0",
  5. "jest": "^24.0.0"
  6. }

While we generally recommend using the same version of every Jest package, this workaround will allow you to continue using the latest version of Jest with Babel 6 for now.

使用 webpack

Jest 可以用于使用 webpack 来管理资源、 样式和编译的项目中。 webpack 与其他工具相比多了一些独特的挑战。 参考 webpack 指南 来开始起步。

Using parcel

Jest can be used in projects that use parcel-bundler to manage assets, styles, and compilation similar to webpack. Parcel requires zero configuration. Refer to the official docs to get started.

使用 TypeScript

Jest supports TypeScript, via Babel. First, make sure you followed the instructions on using Babel above. Next, install the @babel/preset-typescript via yarn:

  1. yarn add --dev @babel/preset-typescript

Then add @babel/preset-typescript to the list of presets in your babel.config.js.

  1. // babel.config.js
  2. module.exports = {
  3. presets: [
  4. ['@babel/preset-env', {targets: {node: 'current'}}],
  5. + '@babel/preset-typescript',
  6. ],
  7. };

不过,在配合使用TypeScript与Babel时,仍然有一些 注意事项 。 因为 Babel 对 TypeScript 的支持是 transpilation,Jest 不会在运行时对你的测试进行类型检查。 If you want that, you can use ts-jest.