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

Is there a way to run some tests sequentially with Jest?

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 )

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

我也需要同样的功能。我有一大套我想要的 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",
    "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-configklaw-syncmicromatch

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

现在,如果您只想 运行 需要按顺序 运行 的测试,您可以 运行 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

使用串行测试运行器:

npm install jest-serial-runner --save-dev

设置 jest 以使用它,例如在 jest.config.js:

module.exports = {
   ...,
   runner: 'jest-serial-runner'
};

您可以使用项目功能将其仅应用于一部分测试。参见 https://jestjs.io/docs/en/configuration#projects-arraystring--projectconfig

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

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)"],
    },
  ],
}

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

这有点提升,但我认为值得发布我的最终配置。我遇到了同样的问题,我扩展了 Joachim Lous' and Fishball's 个答案以获得令人满意的结果。这是我的最终设置:

我的项目的缩写目录列表:

├── src
│   ├── index.ts
│   ├── Io.ts
│   ├── Modules
│   │   └── index.ts
│   └── .....
└── tests
    ├── EndToEnd
    │   ├── Fixtures.ts
    │   ├── Mq.spec.ts
    │   ├── LegalEntities.spec.ts
    │   └── Utils.ts
    └── UnitTests
        ├── LegalEntities.spec.ts
        ├── Assets.spec.ts
        └── Utils.ts

我的(缩写)package.json 文件,里面有我的笑话配置:

{
  "name": "my-project",
  "scripts": {
    "build": "scripts/build",
    "check": "tsc --noEmit",
    "test": "jest"
  },
  "....": "....",
  "jest": {
    "projects": [
      {
        "displayName": "unit-tests",
        "testEnvironment": "node",
        "verbose": true,
        "testMatch": [
          "<rootDir>/tests/**/*.spec.ts",
          "!**/EndToEnd/**/*.spec.ts"
        ],
        "transform": {
          "^.+\.tsx?$": "ts-jest"
        }
      },
      {
        "displayName": "e2e-tests",
        "testEnvironment": "node",
        "verbose": true,
        "maxWorkers": 1,
        "testMatch": [
          "<rootDir>/tests/EndToEnd/**/*.spec.ts"   
        ],
        "transform": {
          "^.+\.tsx?$": "ts-jest"
        }
      }
    ]
  }
}

注意事项:

  • 在玩笑中使用 projects 键时,我不得不将 all 配置移动到各个项目块中。使用项目配置与使用全局配置互斥。
  • 我没有使用其他答案中提到的 runner 指令。相反,我使用 maxWorkers 选项将执行限制为 1 个工作人员(即,本质上是串行的)。 这意味着我不必使用更多的依赖项。
  • 出于某种原因,否定语法对我的单元测试很挑剔。我想将单元测试指定为所有不在 EndToEnd 目录中的测试,并且我花了一些时间试图开玩笑才能正确地做到这一点。

感谢其他所有人提供可行的起点。希望这对其他人有帮助!