我正在通过npm测试运行Jest测试。Jest默认情况下并行运行测试。是否有办法使测试按顺序运行?
我有一些测试调用依赖于更改当前工作目录的第三方代码。
我正在通过npm测试运行Jest测试。Jest默认情况下并行运行测试。是否有办法使测试按顺序运行?
我有一些测试调用依赖于更改当前工作目录的第三方代码。
当前回答
是的,您还可以以特定的顺序运行所有测试,尽管通常您的测试应该是独立的,所以我强烈警告不要依赖任何特定的顺序。说到这里,可能有一个控制测试顺序的有效案例,所以你可以这样做:
Add --runInBand as an option when running jest, e.g. in package.json. This will run tests in sequence rather than in parallel (asynchronously). Using --runInBand can prevent issues like setup/teardown/cleanup in one set of tests intefering with other tests: "scripts": {"test": "jest --runInBand"} Put all tests into separate folder (e.g. a separate folder under __tests__, named test_suites): __tests__ test_suites test1.js test2.js Configure jest in package.json to ignore this test_suites folder: "jest": { "testPathIgnorePatterns": ["/test_suites"] } Create a new file under __tests__ e.g. tests.js - this is now the only test file that will actually run. In tests.js, require the individual test files in the order that you want to run them: require('./test_suites/test1.js'); require('./test_suites/test2.js');
注意——这将导致测试中的afterAll()在所有测试完成后运行。从本质上讲,它破坏了测试的独立性,应该在非常有限的场景中使用。
其他回答
Jest按照在收集阶段遇到的顺序连续运行所有测试
你可以利用它并创建特殊的测试文件alltests.ordered-test.js:
import './first-test'
import './second-test'
// etc.
并添加一个带有testMatch的笑话配置,它将使用该文件名运行测试。
这将按该顺序加载每个文件,从而以相同的顺序执行它们。
这对我来说很有效,确保了模块测试的有序运行:
1)将测试保存在独立的文件中,但没有spec/test的命名。
|__testsToRunSequentially.test.js
|__tests
|__testSuite1.js
|__testSuite2.js
|__index.js
2)测试套件的文件也应该是这样的(testSuite1.js):
export const testSuite1 = () => describe(/*your suite inside*/)
3)将它们导入testtorunsequential .test.js并使用——runInBand运行:
import { testSuite1, testSuite2 } from './tests'
describe('sequentially run tests', () => {
testSuite1()
testSuite2()
})
以防有人想在包中保留所有的笑话配置。json选项。
runInBand似乎不是一个有效的配置选项。这意味着你可以以下面的设置结束,它似乎不是100%完美的。
"scripts": {
"test": "jest --runInBand"
},
...
"jest": {
"verbose": true,
"forceExit": true,
"preset": "ts-jest",
"testURL": "http://localhost/",
"testRegex": "\\.test\\.ts$",
...
}
...
但是,你可以像下面这样使用maxWorkers选项添加runInBand:
"scripts": {
"test": "jest"
},
...
"jest": {
"verbose": true,
"maxWorkers": 1,
"forceExit": true,
"preset": "ts-jest",
"testURL": "http://localhost/",
"testRegex": "\\.test\\.ts$",
...
}
...
如果你是Jest的新手,正在寻找一个完整的,循序渐进的例子,如何让一个特定的测试文件总是在第一个或最后一个运行,下面是:
在你想要的任何路径上创建一个名为“testSequencer.js”的文件。 将下面的代码粘贴到该文件中。
const TestSequencer = require('@jest/test-sequencer').default;
const path = require('path');
class CustomSequencer extends TestSequencer {
sort(tests) {
const target_test_path = path.join(__dirname, 'target.test.js');
const target_test_index = tests.findIndex(t => t.path === target_test_path);
if (target_test_index == -1) {
return tests;
}
const target_test = tests[target_test_index];
const ordered_tests = tests;
ordered_tests.splice(target_test_index, 1);
ordered_tests.push(target_test); // adds to the tail
// ordered_tests.unshift(target_test); // adds to the head
return ordered_tests;
}
}
module.exports = CustomSequencer;
将“maxWorkers”选项设置为“true”。Json笑话配置。另外,将“testSequencer”选项设置为新创建的“testSequencer.js”文件的路径。
{
"name": "myApp",
"version": "1.0.0",
"main": "app.js",
"scripts": {
"start": "node app.js",
"dev": "nodemon app.js",
"test": "jest"
},
"author": "Company",
"license": "MIT",
"dependencies": {
...
},
"devDependencies": {
"jest": "^27.5.1",
...
},
"jest": {
"testSequencer": "./testSequencer.js",
"maxWorkers": 1
}
}
运行npm test并观察每个测试文件将在每个测试完成后逐个运行。你牺牲了一些时间,但你这样做保证了订单。
额外的好处:你还可以按字母顺序、文件夹名等顺序排列测试文件。只需根据自己的喜好修改“testsequcer .js”文件,并返回一个与“tests”数组格式相同的数组,该数组是主“sort”函数的参数,这样就可以了。
摘自https://github.com/facebook/jest/issues/6194#issuecomment-419837314
test.spec.js
import { signuptests } from './signup'
import { logintests } from './login'
describe('Signup', signuptests)
describe('Login', logintests)
signup.js
export const signuptests = () => {
it('Should have login elements', () => {});
it('Should Signup', () => {}});
}
login.js
export const logintests = () => {
it('Should Login', () => {}});
}