我正在通过npm测试运行Jest测试。Jest默认情况下并行运行测试。是否有办法使测试按顺序运行?
我有一些测试调用依赖于更改当前工作目录的第三方代码。
我正在通过npm测试运行Jest测试。Jest默认情况下并行运行测试。是否有办法使测试按顺序运行?
我有一些测试调用依赖于更改当前工作目录的第三方代码。
当前回答
以防有人想在包中保留所有的笑话配置。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文档:
Jest在执行之前执行测试文件中的所有描述处理程序 任何实际的测试。这是另一个建立和的原因 在*和*处理程序之前和之后拆除,而不是在 描述模块。
一旦描述块完成,默认情况下 Jest按顺序连续运行所有测试 遇到在收集阶段,等待每一个完成和被 在继续前进之前整理一下。
看看这个笑话网站给出的例子。
我仍在熟悉Jest,但似乎描述块是同步运行的,而测试块是异步运行的。我在一个外部描述中运行多个描述块,看起来像这样:
describe
describe
test1
test2
describe
test3
在这种情况下,test3直到test2完成才运行,因为test3位于包含test2的描述块之后的描述块中。
Jest按照在收集阶段遇到的顺序连续运行所有测试
你可以利用它并创建特殊的测试文件alltests.ordered-test.js:
import './first-test'
import './second-test'
// etc.
并添加一个带有testMatch的笑话配置,它将使用该文件名运行测试。
这将按该顺序加载每个文件,从而以相同的顺序执行它们。
摘自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', () => {}});
}
我需要它来处理端到端测试和常规测试,而runInBand解决方案对我来说还不够。是的:它确保在测试套件/文件中按照顺序工作,但是文件本身按照Jest为并行化选择的顺序运行,并且不容易控制。如果您需要测试套件本身的稳定顺序,那么您可以这样做。
除了——runInBand之外,我做了以下操作。顺便说一下,我在一个存储库中使用了单独的项目。
My jest.config.js looks like this: module.exports = { testSequencer: "./__e2e__/jest/customSequencer.js", projects: [{ "rootDir": "<rootDir>/__e2e__", "displayName": "end-to-end", ... Here, I explicitly added the displayName to be end-to-end, which I'll use later. You can have as many projects as you like, as usual, but I have two, one for normal unit tests, and one for end-to-end. Note that the testSequencer field has to be global. If you attach it to a project, it'll be validated but then ignored silently. That's a Jest decision to make sequencing nice for running multiple projects. The testSequencer field points to a file containing this. This imports a default version of the test sequencer, and then partitions the tests into two sets, one for the tests in the end-to-end project, and all the rest. All the rest are delegated to the inherited sequencer, but those in the end to end set are sorted alphabetically and then concatenated. const Sequencer = require('@jest/test-sequencer').default; const isEndToEnd = (test) => { const contextConfig = test.context.config; return contextConfig.displayName.name === 'end-to-end'; }; class CustomSequencer extends Sequencer { sort(tests) { const copyTests = Array.from(tests); const normalTests = copyTests.filter((t) => ! isEndToEnd(t)); const endToEndTests = copyTests.filter((t) => isEndToEnd(t)); return super.sort(normalTests).concat(endToEndTests.sort((a, b) => (a.path > b.path ? 1 : -1))); } } module.exports = CustomSequencer;
这个组合像Jest喜欢的那样运行所有常规测试,但总是以alpha顺序运行端到端测试,为用户模型提供他们需要的顺序的端到端测试额外的稳定性。