Search code examples
javascripttestingtimerjestjs

Jest setTimeout not pausing test


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!


Solution

  • 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)
    })