首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于参数列表并行运行jest测试

基于参数列表并行运行jest测试
EN

Stack Overflow用户
提问于 2022-01-31 13:38:09
回答 1查看 818关注 0票数 0

在我的例子中,我有一个测试文件,其中包含了使用jest的几百个测试。

代码语言:javascript
复制
describe('my test-suite', () => {
   test('test 1', () => {
       expect(1).toBe(1);
   });
   //another hundred tests...
});

现在我需要做的是对配置文件中指定的不同参数运行这些测试。更详细的是,在我的例子中,配置文件为外部服务指定了一些外部参数(就像DB访问参数一样),理想情况下,我希望保持完全相同的结构,对所有这些外部服务多次运行相同的测试。

理想情况下,我应该能够做这样的事情:

代码语言:javascript
复制
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),我得到的是:

代码语言:javascript
复制
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".

有什么办法能达到我在这里想做的吗?

EN

回答 1

Stack Overflow用户

发布于 2022-05-01 06:29:00

不能在each循环中运行描述块。在自己的循环中运行每个测试,然后它就可以工作了。就像这样:

代码语言:javascript
复制
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...
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70926907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档