22

I want to verify a function that can get update after one minute, and I set some sleep in my code, but my default time out value is 15000 ms, my code has sleep 60000ms so it returns this error:

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

my code is here:

it('shows that timeline can get updated after one minute', async () => {
    await selectTimeForTimeLine.selectTime('Last 5 minutes');
    await page.waitForTimeout(3000);
    const defaultTime = await alarmTimeLine.xAxisValues();
    await page.evaluate(() => {
      return new Promise((resolve) => setTimeout(resolve, 60000));
    });
    const correntTime = await alarmTimeLine.xAxisValues();
    expect(defaultTime).not.toEqual(correntTime);
  });

Where should I put jest.setTimeOut()? I want to increase exceeded timeout value to 70000ms to make sure my code runs well.

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
Ryan
  • 850
  • 1
  • 5
  • 16
  • 1
    No test should take 70 seconds: _test the functionality you actually need to test_ not a full minute of artificial wait time. – Mike 'Pomax' Kamermans Aug 17 '21 at 04:07
  • this test case I should wait a minute, 70 seconds is jest timeout value, problem is where should I put jest.settimeout, – Ryan Aug 17 '21 at 04:33
  • 1
    You really shouldn't though: what you're testing does not require 70 seconds to pass, it requires your code to _think_ 70 seconds passed. Mock _that_ instead of actually having your test spend over a minute doing nothing: that's absolutely terrible testing. – Mike 'Pomax' Kamermans Aug 17 '21 at 04:35
  • ahh common man , yes, I spend minute doing nothing, but my page update data, checkpoint is timeline can get update after minute or not, – Ryan Aug 17 '21 at 05:14
  • 70 seconds is Exceeded timeout value, actually my test is finish after 60 seconds page get update, – Ryan Aug 17 '21 at 05:21
  • @WaliAblikim Try adding it to the top of the file, before your first test or test suite. Alternatively, you could generalize the piece of code you are testing so it does what it has to after an arbitrary number of seconds. That way your test can execute in a shorter time period. – rexess Aug 17 '21 at 05:33
  • @rexessilfie I have put it to under the "beforeAll(async() => {) but it still return "thrown: "Exceeded timeout of 15000 ms for a test. Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test.", – Ryan Aug 17 '21 at 05:50

4 Answers4

52

To set a timeout on a single test, pass a third option to it/test, for example:

 it('testing balabala', async () => {
   ...
 }, 70000);
mikemaccana
  • 110,530
  • 99
  • 389
  • 494
Ryan
  • 850
  • 1
  • 5
  • 16
9

From the jest.setTimeout() docs:

Set the default timeout interval for tests and before/after hooks in milliseconds. This only affects the test file from which this function is called.

Ie jest.setTimeout() is handled on a file level. Their example doesn't make it clear, but you should have run jest.setTimeout() at the top of your test file:

const SECONDS = 1000;
jest.setTimeout(70 * SECONDS)

describe(`something`, () => {
  it('works', async () => {
    asset(true).isTruthy()
  });
})

Update: I've now sent a PR to the Jest team, which has been accepted, to clarify the docs. They now read:

To set timeout intervals on different tests in the same file, use the timeout option on each individual test.

This is part of Jest 29.5 and newer.

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
5

After upgrading to v28+, this jest.setTimeout() does not work for me anymore. I have to set the timeout explicitly in ./jest.config.js:

// jest.config.js
module.exports = {
    // ...
    testTimeout: 70000
}
LeOn - Han Li
  • 9,388
  • 1
  • 65
  • 59
1

jest also has a config file.

// jest.config.js
module.exports = {
    // ...
    testTimeout: 20000
}
john k
  • 6,268
  • 4
  • 55
  • 59