gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 02:08:48 25 4
gpt4 key购买 nike

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

关于javascript - 有没有办法用 Jest 按顺序运行一些测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47291189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com