27
it('has working hooks', async () => {
  setTimeout(() => {
    console.log("Why don't I run?")
    expect(true).toBe(true)
  }, 15000)

I've already reviewed this answer, Jest documentation, and several GitHub threads:

Disable Jest setTimeout mock

Right now, the function inside the timeout doesn't run. How can I get Jest to pause its execution of the test for 15 seconds and then run the inner function?

Thanks!

Nick
  • 581
  • 1
  • 7
  • 13

3 Answers3

43
it('has working hooks', async () => {
  await new Promise(res => setTimeout(() => {
    console.log("Why don't I run?")
    expect(true).toBe(true)
    res()
  }, 15000))
})

or

it('has working hooks', done => {
  setTimeout(() => {
    console.log("Why don't I run?")
    expect(true).toBe(true)
    done()
  }, 15000)
})
swelet
  • 8,192
  • 5
  • 33
  • 45
Ron
  • 6,037
  • 4
  • 33
  • 52
  • Thank you! Sorry for the late approval. College happened. Can you offer more explanation about why this works, and the error with my code? Also, if anyone has a problem with the above, try lowering the timeout time from 15000 because there's a built in max in the testing framework it exceeds. – Nick Nov 14 '17 at 18:11
  • 1
    In your code, you didn't pass `done` parameter, so `jest` will just run till end , though you have `async`, but you didn't have any `await` to hold the code. I may answer, the 1st one use `async` with `await`, and the 2nd one use `done`, both will hold the code. I hope I explained a little bit. – Ron Nov 15 '17 at 09:23
  • using expect before done just makes it run undefinetly for me. any idea? – Genjuro Dec 06 '17 at 16:32
  • I guess you are using jest in node. In this case, you need to set testEnvironment to node, check https://facebook.github.io/jest/docs/en/configuration.html#testenvironment-string. – Ron Dec 07 '17 at 10:09
  • Using `done()` leads to an eslint error in my case so I went with the first method. Thanks for both methods. – Farzad Soltani Jan 10 '20 at 07:44
  • Can you explain where `done()` comes from? I tried it, but VS Code isn't helping me find it from wherever it's exported. I'm just getting a `Cannot find name 'done'` TypeScript error. – Manpreet Mar 21 '23 at 13:01
15

A nice and clean way to do it (without callbacks) we can simply run an await and pass the res to setTimeout(res, XXX) like so:

it('works with await Promise and setTimeout', async () => {
  // await 15000ms before continuing further
  await new Promise(res => setTimeout(res, 15000));

  // run your test
  expect(true).toBe(true)
});
Mark Pieszak - Trilon.io
  • 61,391
  • 14
  • 82
  • 96
-5

setTimeout is now available through the jest object, and it will function as you expect: https://jestjs.io/docs/jest-object#misc.

it('works with jest.setTimeout', async () => {
  // pause test example for 15 seconds
  jest.setTimeout(15000)

  // run your test
  expect(true).toBe(true)
});

Note: If you attempt to set a timeout for 5000ms or more without the jest object, jest will error and let you know to use jest.setTimeout instead.

colemerrick
  • 1,118
  • 12
  • 17
  • 6
    This does not pause the current test function. jest.setTimeout defines maximum amount of time a test function can take for completion. With a default of 5 seconds a jest test function will fail if it took longer. You can easily proof that if you "add await new Promise(res => setTimeout(res, 15000));" to your test function. – Daniel Ludwig Nov 17 '21 at 08:21