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

Most upvoted comments

You can use the third argument to test fn, isn’t this working for you?

test('example', async () => {
  await new Promise(resolve => setTimeout(resolve, 1000));
}, 500);

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 to test/it, e.g. setting 10 secs timeout for just this one async test:

it('does a lot of stuff exceeding default 5 sec timeout', async () => {
  ...
}, 10000)

pass done into the test and call it after the assertion

it("create or update ",async (**done**)=>
{   
    const res = await request(server)
    .post('...............')
    .send(input);
    expect(res.status).toBe(200);   
   **done();**
});

@thymikee actually, I just ran your example and it didn’t work, eg:

// stuff.test.js
describe('Test', () => {
  test('timeout', async () => {
    await new Promise(resolve => setTimeout(resolve, 1000));
  }, 500);
});
 FAIL  tests/stuff.test.js
  Test
    ✕ timeout (502ms)

  ● Test › timeout

    Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

      at ../../../../../../usr/local/lib/node_modules/jest/node_modules/jest-jasmine2/build/queue_runner.js:64:21
      at ontimeout (timers.js:471:11)
      at tryOnTimeout (timers.js:306:5)
      at Timer.listOnTimeout (timers.js:266:5)

i got an same error

“Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.”

describe(‘POST /…’,()=>{

it("create or update ",async ()=>
{   
    const res = await request(server)
    .post('...............')
    .send(input);
    expect(res.status).toBe(200);   

});

}); … 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 promises

So 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.


test('something', async () => {
  await expect(...).resolves.not.toThrow()
}, 10000) // Async callback was not invoked within the 5000ms timeout...

If I add:


beforeEach(async () => {
  jest.setTimeout(10000)
})

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?

jest.setTimeout(60000)

test('Sys Test No Control', async function() {
...

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