Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setTimeout per test? #5055

Closed
VictorChen opened this issue Dec 11, 2017 · 15 comments · Fixed by #5057
Closed

setTimeout per test? #5055

VictorChen opened this issue Dec 11, 2017 · 15 comments · Fixed by #5057

Comments

@VictorChen
Copy link
Contributor

VictorChen commented Dec 11, 2017

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);
@thymikee
Copy link
Collaborator

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);

@VictorChen
Copy link
Contributor Author

ah interesting, is this not in the documentation?

http://facebook.github.io/jest/docs/en/api.html#testname-fn

@thymikee
Copy link
Collaborator

Looks like it isn't 😅. Would you like to contribute that change to docs?

@VictorChen
Copy link
Contributor Author

@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)

@thymikee
Copy link
Collaborator

It ended after ~500ms, which is shorter that 1000ms your async callback takes, therefore it fails as expected.

@thymikee
Copy link
Collaborator

Closing this. Happy to accept a PR with docs change though :)

@VictorChen
Copy link
Contributor Author

ah you're right, I don't know what I was thinking hehe... I'll update the docs! Thanks!

@JebasinghMathan2311
Copy link

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

@DavidNaMills
Copy link

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();**
});

@SimenB
Copy link
Member

SimenB commented Oct 31, 2018

no need to use done at all if you use promises

@polRk

This comment has been minimized.

@cyberwombat
Copy link

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?

@NateZimmer
Copy link

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.

@demisx
Copy link

demisx commented Nov 25, 2019

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)

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants