Why isn't Jest applying my timeout when I use jest.setTimeout()?

mik*_*ana 5 jestjs

I know I can use jest.setTimeout() to set a custom timeout for a test. I'm doing this below. MINUTE has the value 60 * 1000.

Why isn't Jest applying my timeout?

    thrown: "Exceeded timeout of 5000 ms for a test.
    Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."

      13 |
      14 | describe(`integration with API provider`, () => {
    > 15 |   it(`works`, async () => {
         |   ^
      16 |     // Just in case network is slow.
      17 |     jest.setTimeout(1 * MINUTE);
Run Code Online (Sandbox Code Playgroud)

jon*_*rpe 13

As you've seen (and despite what's claimed elsewhere on SO), you cannot change a single test's timeout by calling jest.setTimeout from inside it. Note that the docs you quote state (emphasis mine):

This only affects the test file from which this function is called.

It's intended to be used at test discovery time, not execution time, to set the timeout for a given context. The timeout is set before the test callback is invoked, you can't change it once the test actually starts.

For a single test you can set the timeout by passing a third argument to the test/it function (or the various helpers defined on it), for example:

it("has a description", () => {
  // ...
}, 60_000);
Run Code Online (Sandbox Code Playgroud)