Jest如何有序地执行测试

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
Jest如何有序地执行测试,Jest如何按照数字顺序执行

项目场景:

node环境下编写js库,处于规范性考虑,需要做单元测试,我选择了Jest

问题描述

       我的js库需要访问数据库,因此操作都是异步的,而且各个测试单元有严格的先后执行顺序(比如,建表 > 插 > 改 > 删),而Jest的每个单元是独立的,并且默认下并行执行测试。
       为此,我查询了解决方案,官方手册和一些博文帖子都告诉我,在jest配置文件中(jest.config.js),设置testSequencer属性,对应的是一个自定义的js模块,它导出了一个按照路径字母的排序算法,如下:

官方手册给出的custom-sequencer.js

const Sequencer = require('@jest/test-sequencer').default;
class CustomSequencer extends Sequencer {
  sort(tests) {
    // Test structure information
    // https://github.com/facebook/jest/blob/6b8b1404a1d9254e7d5d90a8934087a9c9899dab/packages/jest-runner/src/types.ts#L17-L21
    const copyTests = Array.from(tests);
    return copyTests.sort((testA, testB) => (testA.path > testB.path ? 1 : -1));
  }
}
module.exports = CustomSequencer;

相应的jest.config.js配置

/** @type {import('jest').Config} */
const config = {
  testSequencer: 'path/to/custom-sequencer.js',
};

module. Exports = config;

       我照做了,但我的单元依然随机地并行执行,或只是最初加载时在表面上排好了队,即便我设置maxConcurrency为1(A number limiting the number of tests that are allowed to run at the same time when using test.concurrent. Any test above this limit will be queued and executed once a slot is released.)。在有一些帖子的回答中,有人则认为,Jest难以支持指定顺序,或者要额外写不少代码才能实现Jest有序测试。


解决方案:

仅仅为jest指定排序算法是不够的,因为默认运行模式就是并行执行!
同时,应该将jest设置为串行运行,这样测试单元才会按照预期的顺序执行

你可以在运行时加上runInBand参数npm test -- --runInBand

jest按序执行测试
可以看到,对于我的四个测试文件,.1.dbIns.tes.js等等,从1-4执行了

你也可以在package.json中为npm脚本添加这个参数:

"scripts": {
    "test": "jest",
    "test-inline": "jest \"--runInBand\"",	//注意是 "--runInBand" 而不是 -- --runInBand,而且一定要转义双引号
    "babel-build": "babel lib/sql.js -d dist"
  },

或者在package.json中,设置maxWorkers为1:
( 官方手册:Specifies the maximum number of workers the worker-pool will spawn for running tests. In single run mode, this defaults to the number of the cores available on your machine minus one for the main thread)
简而言之,你可以认为maxWorkers=1时Jest为单线程

const config = {
    reporters: [~
        "default",
        "jest-allure"
    ],
    setupFilesAfterEnv: ["jest-allure/dist/setup"],
    testRunner: "jest-jasmine2",
    testSequencer: './jest.sequencer.js',
    maxWorkers: 1,
};

module.exports = config;

官方给出的排序法,是按文件路径字母排的序,我个人不喜欢用a,b,aa,ab为文件当作前缀,看着很难受,没有数字来的直观,如果你和我一样,可以使用我改写的排序模块:
此外,你的测试文件需要按照上图我那样子命名:.1.xxx.test.js

const Sequencer = require('@jest/test-sequencer').default;

class CustomSequencer extends Sequencer {
    /**
     * Sort test to determine order of execution
     * Sorting is applied after sharding
     */
    sort(tests) {
        // Test structure information
        // https://github.com/facebook/jest/blob/6b8b1404a1d9254e7d5d90a8934087a9c9899dab/packages/jest-runner/src/types.ts#L17-L21
        const copyTests = Array.from(tests);
        return copyTests.sort((testA, testB) => (parseInt(testA.path.split('.').reverse()[3]) > parseInt(testB.path.split('.').reverse()[3]) ? 1 : -1));
    }
}

module.exports = CustomSequencer;

题外话

写完这篇文后,我在stackoverflow上看到了相同的诉求,并且有人给出了正确的解答。那叫一个后悔啊,我怎么没早点看到

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6