微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/js/157441.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐