javascript – 有没有办法用Jest顺序运行一些测试?

认情况下,Jest并行运行您的测试套件,但有一个标志( – runInBand)允许您按顺序运行整个套件(如 here所指出)

我有一些不能并行运行的测试,但是顺序运行整个套件需要花费更长的时间,所以我的问题是是否有办法只运行一些测试(例如为这些测试设置一个标志或类似的东西).

解决方法

我也需要相同的功能.我有一大堆我想要运行的Jest集成测试套件.但是,由于需要设置和拆除共享资源,有些不能并行运行.所以,这是我提出的解决方案.

我更新了我的package.json脚本:

{
  ...
  "scripts": {
    ...
    "test": "npm run test:unit && npm run test:integration","test:integration": "jest --config=__tests__/integration/jest.config.js","test:unit": "jest --config=__tests__/unit/jest.config.js"
  },...
}

{
  ...
  "scripts": {
    ...
    "test": "npm run test:unit && npm run test:integration","test:integration": "npm run test:integration:sequential && npm run test:integration:parallel","test:integration:parallel": "jest --config=__tests__/integration/jest.config.js","test:integration:sequential": "jest --config=__tests__/integration/jest.config.js --runInBand",...
}

然后我更新了__tests __ / integration / jest.config.js

module.exports = {
  // Note: rootDir is relative to the directory containing this file.
  rootDir: './src',setupFiles: [
    '../setup.js',],testPathIgnorePatterns: [
    ...
  ],};

const Path = require('path');

const { defaults } = require('jest-config');
const klawSync = require('klaw-sync')
const mm = require('micromatch');

// Note: rootDir is relative to the directory containing this file.
const rootDir = './src';
const { testMatch } = defaults;

// Todo: Add the paths to the test suites that need to be run
// sequentially to this array.
const sequentialTestPathMatchPatterns = [
  '<rootDir>/TestSuite1ToRunSequentially.spec.js','<rootDir>/TestSuite2ToRunSequentially.spec.js',...
];

const parallelTestPathIgnorePatterns = [
  ...
];

let testPathIgnorePatterns = [
  ...parallelTestPathIgnorePatterns,...sequentialTestPathMatchPatterns,];

const sequential = process.argv.includes('--runInBand');
if (sequential) {
  const absRootDir = Path.resolve(__dirname,rootDir);
  let filenames = klawSync(absRootDir,{ nodir: true })
    .map(file => file.path)
    .map(file => file.replace(absRootDir,''))
    .map(file => file.replace(/\\/g,'/'))
    .map(file => '<rootDir>' + file);
  filenames = mm(filenames,testMatch);
  testPathIgnorePatterns = mm.not(filenames,sequentialTestPathMatchPatterns);
}

module.exports = {
  rootDir,testMatch,testPathIgnorePatterns,};

更新的jest.config.js取决于jest-config,klaw-sync和micromatch.

npm install --save-dev jest-config klaw-sync micromatch

现在,您可以运行npm run test:integration:sequential如果您只想运行需要按顺序运行的测试.

或运行npm run test:integration:parallel for parallel tests.

或者运行npm run test:integration来首先运行顺序测试.然后,当完成时,并行测试将运行.

或者运行npm run test来运行单元测试和集成测试.

注意:我在单元和集成测试中使用的目录结构如下:

__tests__
  integration
    src
      *.spec.js
      *.test.js
    jest.config.js
  unit
    src
      *.spec.js
      *.test.js
    jest.config.js

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...