Skip to main content
Version: 26.x

入门

使用 yarn 安装 Jest︰

yarn add --dev jest

npm

npm install --save-dev jest

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

让我们从写一个两个数相加的示例函数开始。 首先,创建一个 sum.js 文件︰

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

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

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

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

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

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

PASS ./sum.test.js
✓ 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 作为一个配置文件、并在运行完成后显示一个原生的操作系统通知。

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

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

更多配置#

生成一个基础配置文件#

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

jest --init

使用 Babel#

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

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

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

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

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. You can use that in your configuration to conditionally setup only the compilation needed for Jest, e.g.

// babel.config.js
module.exports = api => {
const isTest = api.env('test');
// You can use isTest to determine what presets and plugins to use.
return {
// ...
};
};
return {
// ...
};
};

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

// jest.config.js
module.exports = {
transform: {},
};
**Babel 6 支持**

Jest 24 dropped support for Babel 6. We highly recommend you to upgrade to Babel 7, which is actively maintained. 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:

"dependencies": {
"babel-core": "^6.26.3",
"babel-jest": "^23.6.0",
"babel-preset-env": "^1.7.0",
"jest": "^24.0.0"
}

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. 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:

yarn add --dev @babel/preset-typescript

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

// babel.config.js
module.exports = {
presets: [
['@babel/preset-env', {targets: {node: 'current'}}],
+ '@babel/preset-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: Because TypeScript support in Babel is purely transpilation, Jest will not type-check your tests as they are run. You may also want to install the @types/jest module for the version of Jest you're using. This will help provide full typing when writing your tests with TypeScript.

不过,在配合使用TypeScript与Babel时,仍然有一些 注意事项 。 Because TypeScript support in Babel is purely transpilation, Jest will not type-check your tests as they are run. If you want that, you can use ts-jest instead, or just run the TypeScript compiler tsc separately (or as part of your build process).

For @types/* modules it's recommended to try to match the version of the associated module. For @types/* modules it's recommended to try to match the version of the associated module. For example, if you are using 26.4.0 of jest then using 26.4.x of @types/jest is ideal. In general, try to match the major (26) and minor (4) version as closely as possible. In general, try to match the major (26) and minor (4) version as closely as possible.

yarn add --dev @types/jest