是否可以使用Jest顺序运行一些测试? [英] Is there a way to run some tests sequentially with Jest?

查看:265
本文介绍了是否可以使用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)

我有一些不能并行运行的测试,但是按顺序运行整个套件的总时间要长得多,所以我的问题是是否有办法只以这种方式运行 some 测试(例如为这些测试设置标志或类似的东西)。

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"
  },
  ...
}

然后我更新了 __ 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,
  setupFiles: [
    '../setup.js',
  ],
  testMatch,
  testPathIgnorePatterns,
};

更新后的 jest.config.js jest-config klaw-sync micromatch

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运行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 来运行两个单元

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

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天全站免登陆