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

Mar*_*o F 12 javascript jestjs create-react-app

开玩笑运行测试套件默认情况下并行,但有一个标志(--runInBand),它允许您运行整个套件顺序(如指出,在这里)

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

Dan*_*urt 8

我也需要相同的功能.我有一大堆我希望运行的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"
  },
  ...
}
Run Code Online (Sandbox Code Playgroud)

{
  ...
  "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"
  },
  ...
}
Run Code Online (Sandbox Code Playgroud)

然后我更新__tests__/integration/jest.config.js

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

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,
};
Run Code Online (Sandbox Code Playgroud)

更新jest.config.js依赖于jest-config,klaw-syncmicromatch.

npm install --save-dev jest-config klaw-sync micromatch
Run Code Online (Sandbox Code Playgroud)

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

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

或者运行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
Run Code Online (Sandbox Code Playgroud)

  • 这么简单的事情就这么麻烦,Jest 确实缺乏非常重要的功能来标记描述块要按顺序运行。 (2认同)

Fis*_*all 5

从Joachim Lous的答案中扩展,您可以将测试文件划分为项目,并为每个项目指定不同的运行程序。

jest.config.js

module.exports = {
  projects: [
    {
      displayName: "default-tests",
      testEnvironment: "node",
    },
    {
      displayName: "serial-tests",
      testEnvironment: "node",
      runner: "jest-serial-runner",
      testMatch: ["**/?(*.)+(serial-test).[jt]s?(x)"],
    },
  ],
}
Run Code Online (Sandbox Code Playgroud)

然后,将需要按顺序运行的任何测试重命名为*.serial-test.js(而不是*.test.js)。