Getting Started

    npm

    1. npm install --save-dev jest

    注:Jest的文档统一使用yarn命令,不过使用npm也是可行的。 你可以在里看到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. "test": "jest"
    3. }
    4. }

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

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

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

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

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

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

    更多配置

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

    1. jest --init

    如果需要使用 ,可以通过 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. ],

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

    Making your Babel config jest-aware

    Jest will set 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. // 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.

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

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

    Jest supports TypeScript, via Babel. First, make sure you followed the instructions on 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. ],

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