跳到主要内容

快速入门

首先用你喜欢的软件包管理工具来安装 Jest:

npm install --save-dev jest

下面我们开始给一个假定的函数写测试,这个函数的功能是两数相加。首先创建 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 test 或者 npm test ,Jest 将输出如下信息:

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

你刚才使用 Jest 成功地写出了第一个测试!

在此测试中,使用了 expecttoBe 来检测两个值是否完全相同。若要了解其它使用 Jest 可以测试的内容,请参阅使用匹配器(Matcher)

使用命令行

你可以直接从 CLI 运行 Jest 并使用一系列有用的配置参数(前提是已经配置了环境变量 PATH 使其全局可用,例如通过 yarn global add jest 或者 npm install jest --global 来安装 Jest)。

这里演示了如何对于能够匹配到 my-test 的文件运行 Jest,以 config.json 作为配置文件,并在运行后显示一个基于原生操作系统的通知:

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

如果想要了解更多在命令行中运行 jest 的内容,请参阅 Jest CLI 配置项页面.

更多配置项

生成基础配置文件

Jest 将根据你的项目提出一系列问题,并且将创建一个基础配置文件。文件中的每一项都配有简短的说明:

jest --init

使用 Babel

要使用 Babel,请首先安装所需的依赖项:

npm install --save-dev babel-jest @babel/core @babel/preset-env

在项目的根目录下创建 babel.config.js ,通过配置 Babel 使其能够兼容当前的 Node 版本。

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

Babel 理想的配置取决于项目本身。 请查阅 Babel 官方文档 来获得更多详细信息。

将 Babel 配置为 Jest 可感知的

如果 process.env.NODE_ENV 没有被设置成其它值,Jest 会把它设置成 'test' 。你可以运用在配置项中,从而根据实际情况设定适用于 Jest 的编译。例如:

babel.config.js
module.exports = api => {
const isTest = api.env('test');
// 你可以使用 isTest 来决定需要使用到的预设和插件。

return {
// ...
};
};
备注

babel-jest is automatically installed when installing Jest and will automatically transform files if a babel configuration exists in your project. To avoid this behavior, you can explicitly reset the transform configuration option:

jest.config.js
module.exports = {
transform: {},
};

使用 webpack

Jest 可以用于在使用 webpack 管理资源、样式和编译方式的项目中。webpack 和其它工具相比的确多了一些独特的挑战。请参阅 webpack 指南 快速上手。

Using Vite

Jest can be used in projects that use vite to serves source code over native ESM to provide some frontend tooling, vite is an opinionated tool and does offer some out-of-the box workflows. Jest is not fully supported by vite due to how the plugin system from vite works, but there is some working examples for first-class jest integration using the vite-jest, since this is not fully supported, you might as well read the limitation of the vite-jest. Refer to the vite guide to get started. Alternatively you can use vitest.

使用 Parcel

Jest 可以用在使用 parcel-bundler 的项目中,用于管理静态资源、样式和编译,和 webpack 的用法类似。Parcel 无需配置。请参考 Parcel 的官方 文档 开始吧。

使用 Typescript

通过 babel 来支持 Typescript

通过 Babel,Jest 能够支持 Typescript。首先要确保你遵循了上述 使用 Babel 指引。接下来安装 @babel/preset-typescript 插件:

npm install --save-dev @babel/preset-typescript

然后将 @babel/preset-typescript 添加到 babel.config.js 中的 presets 列表中。

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

不过,将 TypeScript 和 Babel 一起使用时有一些 注意事项 。由于 Babel 对 Typescript 的支持是通过代码转换(Transpilation)实现的,而 Jest 在运行时并不会对你的测试用例做类型检查。 如果你需要此功能,可以使用 ts-jest ,或者单独(或作为构建流程的一部分)直接运行 TypeScript 编译器 tsc

Via ts-jest

ts-jest is a TypeScript preprocessor with source map support for Jest that lets you use Jest to test projects written in TypeScript.

npm install --save-dev ts-jest

In order for Jest to transpile TypeScript with ts-jest, you may also need to create a configuration file.

Type definitions

There are two ways to have Jest global APIs typed for test files written in TypeScript.

You can use type definitions which ships with Jest and will update each time you update Jest. Simply import the APIs from @jest/globals package:

sum.test.ts
import {describe, expect, test} from '@jest/globals';
import {sum} from './sum';

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

See the additional usage documentation of describe.each/test.each and mock functions.

Or you may choose to install the @types/jest package. It provides types for Jest globals without a need to import them.

npm install --save-dev @types/jest
信息

@types/jest is a third party library maintained at DefinitelyTyped, hence the latest Jest features or versions may not be covered yet. Try to match versions of Jest and @types/jest as closely as possible. For example, if you are using Jest 27.4.0 then installing 27.4.x of @types/jest is ideal.