jest: setTimeout per test?
Sorry if this is obvious, but I couldn’t find how to do this on the website. Basically, I want to set a different default timeout for each test, eg:
test('example one', async () => {
const data = await testStuff(1);
expect(data).toEqual('One');
// give up after 5 seconds
});
test('example two', async () => {
const data = await testStuff(2);
expect(data).toEqual('Two');
// give up after 10 seconds
});
Do I just call jest.setTimeout(time);
inside each test
like this:
test('example one', async () => {
jest.setTimeout(5000);
...
});
test('example two', async () => {
jest.setTimeout(10000);
...
});
? Or is there a better way? Would be nice if you could do something like:
test('example', async () => {
...
}, 5000);
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 13
- Comments: 15 (4 by maintainers)
Commits related to this issue
- build: set timeout to slow builds https://github.com/facebook/jest/issues/5055\#issuecomment-526852748 — committed to KengoTODA/gradle-semantic-release-plugin by KengoTODA 4 years ago
- build: set timeout to slow builds https://github.com/facebook/jest/issues/5055\#issuecomment-526852748 — committed to KengoTODA/gradle-semantic-release-plugin by KengoTODA 4 years ago
- build: set timeout to slow builds https://github.com/facebook/jest/issues/5055\#issuecomment-526852748 — committed to KengoTODA/gradle-semantic-release-plugin by KengoTODA 4 years ago
You can use the third argument to
test
fn, isn’t this working for you?I believe
jest.setTimeout(60000)
will set the timeout globally per suite, not per a given test. To set it per individual test, one has to pass it as an additional parameter totest/it
, e.g. setting 10 secs timeout for just this one async test:pass done into the test and call it after the assertion
@thymikee actually, I just ran your example and it didn’t work, eg:
i got an same error
“Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.”
describe(‘POST /…’,()=>{
}); … means does not show the url
Somebody have a solution ,pls send me
It ended after ~500ms, which is shorter that 1000ms your async callback takes, therefore it fails as expected.
no need to use
done
at all if you use promisesSo a StackOverflow answer says you cannot set a timeout in the test function that is greater than the default timeout - I cannot find documentation on this but my own test show that the timeout I put in the test fn will be ignored.
If I add:
Then it works fine. I am on 24.8.0.
Thoughts? Where can I find this in docs and if it’s true as it appears then why? What is the point of not being able to set a timer greater than default?
Looks like it isn’t 😅. Would you like to contribute that change to docs?
This worked for me. Cleaner than a hackey undocumented 3rd. parameter to a function. CLI instructions ineed to be updated… they say to change
jest.setTimeout.Timeout
which, setting directly doesn’t work.ah interesting, is this not in the documentation?
http://facebook.github.io/jest/docs/en/api.html#testname-fn