【问题标题】:Does jest.setTimeOut() inside a describe block apply the timeout only to tests inside the describe block描述块内的 jest.setTimeOut() 是否仅将超时应用于描述块内的测试
【发布时间】:2020-10-15 17:06:07
【问题描述】:

我正在尝试增加描述块内的某些测试用例的开玩笑超时。

我发现this stackoverflow 问题在哪里他们建议在test(name, fn) 中使用jest.setTimeOut(10000) 来增加该测试用例的超时时间。

test('descriptinon', async () => {
  jest.setTimeOut(10000);
  ...
})

如果我想增加描述块内所有测试的超时时间,我可以在描述块内添加jest.testTimeOut,如下所示?

describe('description', () => {
 jest.setTimeOut(10000);

 test('test description', async () => {
   ...
 });

 test('test description', async () => {
   ...
 });
});

是只增加 describe 块内测试用例的超时时间还是全局增加超时时间?

【问题讨论】:

    标签: javascript jestjs automated-tests


    【解决方案1】:

    根据jest.testTimeOut 上的文档:

    这只影响调用该函数的测试文件。

    文档进一步建议:

    如果你想为所有的测试文件设置超时时间,最好在 setupFilesAfterEnv

    文档中的示例:

    i) 在jest.config.js 中初始化setupFilesAfterEnv

    module.exports = {
      setupFilesAfterEnv: ['./jest.setup.js'],
    };
    

    ii) 在jest.setup.js 中设置项目范围的超时

    jest.setTimeout(10000);
    

    来源:

    https://jestjs.io/docs/en/jest-object.html#jestsettimeouttimeout
    https://jestjs.io/docs/en/configuration#setupfilesafterenv-array

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多