简体   繁体   English

描述块内的 jest.setTimeOut() 是否仅将超时应用于描述块内的测试

[英]Does jest.setTimeOut() inside a describe block apply the timeout only to tests inside the describe block

I am trying to increase the jest timeout for some test cases all inside a describe block.我正在尝试为描述块内的某些测试用例增加开玩笑的超时时间。

I found this stackoverflow question where in answer they suggested using jest.setTimeOut(10000) inside test(name, fn) to increase timeout for just that test case.我发现了这个stackoverflow 问题,他们建议在test(name, fn)中使用jest.setTimeOut(10000)来增加该测试用例的超时时间。

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

If I want to increase the timeout for all the tests inside a describe block can I just add jest.testTimeOut inside the describe block as follows?如果我想增加 describe 块内所有测试的超时时间,我可以在 describe 块内添加jest.testTimeOut ,如下所示?

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

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

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

Does it only increase timeout for test cases inside the describe block or does it increase the timeout globally?它是只增加描述块内的测试用例的超时时间还是增加全局超时时间?

According to the docs on jest.testTimeOut :根据jest.testTimeOut上的文档:

This only affects the test file from which this function is called.这只会影响调用此 function 的测试文件。

The docs further suggest that:文档进一步建议:

If you want to set the timeout for all test files, a good place to do this is in setupFilesAfterEnv如果您想为所有测试文件设置超时,那么在setupFilesAfterEnv中执行此操作的好地方

Example from the docs:文档中的示例:

i) Initialise setupFilesAfterEnv in jest.config.js : i) 在jest.config.js

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

ii) Set timeout project-wide in jest.setup.js ii) 在jest.setup.js中设置项目范围的超时

jest.setTimeout(10000);

Sources:资料来源:

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

Unfortunately setting jest.setTimeout(10000) in a describe block will apply to tests outside the scope of that describe block.不幸的是,在描述块中设置jest.setTimeout(10000)将应用于该描述块的 scope 之外的测试。

One solution is to set a beforeEach and afterEach in the describe block that sets the timeout to the increased value before each test, and then updates it back to the default value (5000, or whatever you have configured) after each test finishes.一种解决方案是在 describe 块中设置beforeEachafterEach ,在每次测试之前将超时设置为增加的值,然后在每次测试完成后将其更新回默认值(5000 或您配置的任何值)。

Example showing that setTimeout affects tests outside the describe:显示 setTimeout 影响描述之外的测试的示例:

describe('Timeout Shenanigans', () => {
  describe('Custom Timeout 10000', () => {
    jest.setTimeout(10000)
    it('Should Pass, Under Custom Timeout', async () => {
      await delay(7500);
    })
    it('Should Fail, Over Custom Timeout', async () => {
      await delay(11000);
    })
  });
  describe('Default Timeout 5000', () => {
    it('Should Fail, Over Default Timeout', async () => {
      await delay(5500);
    })
    it('Should Pass, Under Default Timeout', async () => {
      await delay(4900);
    })
    it('Should Pass, Specifies Own 12000 Timeout', async () => {
      await delay(11000);
    }, 12000)
  });
});

=> =>

  Timeout Shenanigans
    Custom Timeout 10000
      ✓ Should Pass, Under Custom Timeout (7502 ms)
      ✕ Should Fail, Over Custom Timeout (10005 ms)
    Default Timeout 5000
      ✓ Should Fail, Over Default Timeout (5504 ms)
      ✓ Should Pass, Under Default Timeout (4904 ms)
      ✓ Should Pass, Specifies Own 12000 Timeout (11005 ms)

Notice the first 5000 case passes when it should have failed for exceeding the timeout, even though jest.setTimeout is called in the other describe block.请注意,前 5000 个案例在它应该因超过超时而失败时通过,即使在另一个描述块中调用了jest.setTimeout This can't be solved by re-ordering the describe blocks.这不能通过重新排序描述块来解决。

Example showing the beforeEach / afterEach solution:显示 beforeEach / afterEach 解决方案的示例:

describe('Timeout Shenanigans', () => {
  describe('Default Timeout 5000', () => {
    it('Should Fail, Over Default Timeout', async () => {
      await delay(5500);
    })
    it('Should Pass, Under Default Timeout', async () => {
      await delay(4900);
    })
    it('Should Pass, Specifies Own 12000 Timeout', async () => {
      await delay(11000);
    }, 12000)
  });
  describe('Custom Timeout 10000', () => {
    beforeEach(() => {
      jest.setTimeout(10000);
    });
    afterEach(() => {
      jest.setTimeout(5000);
    })
    it('Should Pass, Under Custom Timeout', async () => {
      await delay(7500);
    })
    it('Should Fail, Over Custom Timeout', async () => {
      await delay(11000);
    })
  });
});

=> =>

  Timeout Shenanagins
    Default Timeout 5000
      ✕ Should Fail, Over Default Timeout (5003 ms)
      ✓ Should Pass, Under Default Timeout (4903 ms)
      ✓ Should Pass, Specifies Own 12000 Timeout (11005 ms)
    Custom Timeout 10000
      ✓ Should Pass, Under Custom Timeout (7506 ms)
      ✕ Should Fail, Over Custom Timeout (10001 ms)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 开玩笑-describe块的顺序执行 - Jest - Sequential Execution of a describe block Jest - 在 describe 块中导入多个测试,重用 beforeEach() 中定义的变量 - Jest - import multiple tests in a describe block, reusing variables defined in beforeEach() 茉莉花-如何设置describe块的超时时间? - Jasmine - How to set a timeout for a describe block? 如何从Mocha describe块内部导出变量 - How to export a variable from inside a Mocha describe block 在 Jest describe 块中,使用 getElementsByClassName 时一次只能通过一个测试 - In Jest describe block, only one test passes at a time when using getElementsByClassName Jest - 如何让 describe 块访问 beforeAll 中分配的变量? - Jest - How to give describe block access to variables assigned in beforeAll? 有没有办法计算jasmine describe块中的测试数量? - Is there a way to get the count for the number of tests in a jasmine describe block? 运行jasmine测试时,我怎么知道我是否在一个描述块中,在每个块之前还是阻塞? - When running jasmine tests, how can I know if I am in a describe block, beforeEach block or it block? jest.setTimeout在setupFiles中不起作用 - jest.setTimeout is not working in setupFiles 使用 Node 进行 Jest 测试 - 超时 - 在 jest.setTimeout 指定的 5000 毫秒超时内未调用异步回调 - Jest testing with Node - Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM