有没有办法用 Jest 按顺序运行一些测试? [英] Is there a way to run some tests sequentially with Jest?

查看:32
本文介绍了有没有办法用 Jest 按顺序运行一些测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Jest 默认情况下并行运行您的测试套件,但有一个标志(--runInBand) 允许您按顺序运行整个套件(如这里)

Jest runs your test suite in parallel by default, but there is a flag (--runInBand) that allows you to run the whole suite sequentially (as pointed out here)

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

I have some tests that cannot run in parallel, but running the whole suite sequentially takes a lot longer in total, so my question is if there is way to only run some tests that way (such as setting a flag for those tests or something similar).

推荐答案

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

I too needed the same functionality. I have a large set of Jest integration test suites I want to run. However, some can't be run in parallel due to the need of setup and teardown of a shared resource. So, here is the solution I came up with.

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

I updated my package.json scripts from:

{
  ...
  "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",
    "test:unit": "jest --config=__tests__/unit/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,
  setupFiles: [
    '../setup.js',
  ],
  testMatch,
  testPathIgnorePatterns,
};

更新后的jest.config.js依赖于jest-configklaw-syncmicromatch.

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

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

Now, you can run npm run test:integration:sequential if you only want to run the tests that need to be run sequentially.

或者运行 npm run test:integration:parallel 进行并行测试.

Or run npm run test:integration:parallel for the parallel tests.

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

Or run npm run test:integration to first run the sequential tests. Then when that is finished, the parallel tests will run.

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

Or run npm run test to run both the unit and integration tests.

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

Note: The directory structure I am using with my unit and integration tests is as follows:

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

这篇关于有没有办法用 Jest 按顺序运行一些测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆