在我的例子中,我有一个测试文件,其中包含了使用jest的几百个测试。
describe('my test-suite', () => {
test('test 1', () => {
expect(1).toBe(1);
});
//another hundred tests...
});现在我需要做的是对配置文件中指定的不同参数运行这些测试。更详细的是,在我的例子中,配置文件为外部服务指定了一些外部参数(就像DB访问参数一样),理想情况下,我希望保持完全相同的结构,对所有这些外部服务多次运行相同的测试。
理想情况下,我应该能够做这样的事情:
const paramList = [{
name: 'service 1'
}, {
name: 'service 2'
}];
describe('my parallel test-suite', () => {
it.each(paramList)("testing against $name", ({name}) => {
//just what I had before
describe('my test-suite', () => {
test('test 1', () => {
expect(1).toBe(1);
});
//another hundred tests...
});
});
});理想情况下,下一步应该是用test.each替换test.concurrent.each,并能够针对所有不同的外部服务并行运行我的测试。
不幸的是,使用上面的代码(和JestVersion27.4.7),我得到的是:
Cannot nest a describe inside a test. Describe block "my test-suite" cannot run because it is nested within "service service 1".
Tests cannot be nested. Test "test 1" cannot run because it is nested within "service service 1".有什么办法能达到我在这里想做的吗?
发布于 2022-05-01 06:29:00
不能在each循环中运行描述块。在自己的循环中运行每个测试,然后它就可以工作了。就像这样:
describe('my parallel test-suite', () => {
it.each(paramList)("testing against $name", ({name}) => {
expect(1).toBe(1);
});
it.each(paramList)("another test against $name", ({name}) => {
expect(2).toBe(2);
});
//another hundred tests...
});https://stackoverflow.com/questions/70926907
复制相似问题