jest: Exceeded timeout of 5000 ms for a test

I am upgrading jest from v1.4.3 --> 2+, with chromedriver version 91+. I have started getting this issue:

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."

I have been googling a lot but not getting proper solution. So, far I have tried following solutions:- 1)Increased jest.setTimeout(30000) from 30000 to 60000. 2)Tried using jest- testRunner": “jest-circus/runner”

My package.json configuration is as below: “@types/jest”: “26.0.23”, “@types/node”: “15.12.4”, “@types/selenium-webdriver”: “4.0.14”, “chromedriver”: “91.0.1”, “concurrently”: “6.2.0”, “jest”: “27.0.5”, “prettier”: “2.3.1”, “selenium-webdriver”: “4.0.0-alpha.7”, “ts-jest”: “27.0.3”, “tslint”: “6.1.3”, “tslint-config-prettier”: “^1.15.0”, “tslint-config-sparta”: “0.3.4”, “tslint-loader”: “3.5.4”, “typedoc”: “0.21.0”, “typescript”: “3.9.7”, “ui-testing-core”: “0.15.12”

Can anybody point out what is the solution?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 38
  • Comments: 55 (2 by maintainers)

Most upvoted comments

// jest.config.js
module.exports = {
    // ...
    testTimeout: 20000
}

It seems to solve the problem.

Same problem, some solution is to add: jest.useFakeTimers(‘legacy’)

I ran @cBiscuitSurprise’s repro and was able to reproduce the issue with jest v27

Looks like jest.setTimeout is broken with that version

jest.useFakeTimers(‘legacy’)

Did not work for me

Reading the changes to v27 here: https://jestjs.io/blog/2021/05/25/jest-27

jest.useRealTimers(); helped a bit, in the repro above, the first test passed with that option activated

then for the other

import { shortProcess, longProcess } from '../../src/index';

jest.useRealTimers();

describe(`something`, function () {
    it('should finish fine', async function () {
        await shortProcess();
        expect(1).toBe(1);
    });

    it('should fail with a timeout', async function () {
        await longProcess();
        expect(1).toBe(1);
    });

    it('should finish fine again', async function () {
        jest.setTimeout(10 * 1000); // not currently working...
        await longProcess();
        expect(1).toBe(1);
    }, 10000);
});

The 10000 in the it block made the third test pass

Looks like we have a workaround for now?

Finally managed to increase the timeout by passing a third parameter to test:

test('my test', () => {
  // Your test logic...
}, 8000)

Both of them worked for me.

  • testTimeout: 20000
  • jest.setTimeout(30000); before the describe block

Increase Jest Timeout and it will work:

const app = require('./server');
const request = require('supertest');

jest.setTimeout(30000);

describe('App', () => {
    it('says Hello World', async () => {
        const res = await request(app).get('/');
        expect(res.body.message).toEqual('Hello World!');
    });
});

jest.setTimeout(30000) outside of any block worked for me. it wasn’t working when i put it in the beforeEach

jest.setTimeout(30000); before the describe block works for me. Others didn’t work.

As noted in the Bug Report template, all bug reports requires a minimal reproduction. Please open up a new issue providing one. Read more at https://stackoverflow.com/help/minimal-reproducible-example.

adding jest.setTimeout(30000); before the describe block worked for me.

  1. It wasn’t working when I added it in the beforeEach or beforeAll hooks.
  2. jest.useRealTimers(); didn’t also work for me.

I’ve tried every single thing besides setting an explicit timeout for each it. The only thing that worked for me was setting "timers": "legacy" in the config file!

I had the 5000 ms timeout error in one of my tests, and it kept occurring even after setting testTimeout: 10000 in my Jest config (the error message kept on saying “5000 ms”). Fortunately, in my case, calling jest.setTimeout(10000) in the failing test made it pass – it’s setting testTimeout in the config that didn’t. I’m using Jest 27.2 on Windows 10.

EDIT Interestingly, on Linux, it seems to be the opposite: testTimeout works but jest.setTimeout doesn’t!

Reading the changes to v27 here: https://jestjs.io/blog/2021/05/25/jest-27

jest.useRealTimers(); helped a bit, in the repro above, the first test passed with that option activated

then for the other

import { shortProcess, longProcess } from '../../src/index';

jest.useRealTimers();

describe(`something`, function () {
    it('should finish fine', async function () {
        await shortProcess();
        expect(1).toBe(1);
    });

    it('should fail with a timeout', async function () {
        await longProcess();
        expect(1).toBe(1);
    });

    it('should finish fine again', async function () {
        jest.setTimeout(10 * 1000); // not currently working...
        await longProcess();
        expect(1).toBe(1);
    }, 10000);
});

The 10000 in the it block made the third test pass

Looks like we have a workaround for now?

It works like a champ

In my case I added a jest attribute in the package.json with the testTimeout property set a little higher have a look :

code

I can confirm that for my case, testTimeout: any number worked.

Thank you very much guys

In my React Native project I had an issue where Jest was failing on .start() in my animation function:

return Animated.timing(animatedValue, {
  toValue,
  duration,
  delay,
  useNativeDriver: true,
  easing: Easing.bezier(0.5, 0.01, 0, 1),
}).start();

With the error: 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."

So I added this after my imports in the Test file:

afterEach(() => {
  jest.useRealTimers();
});

Then I added this to each test case:

jest.useFakeTimers('legacy');

Now it’s working again.

Heres an example:

import { render } from '@testing-library/react-native';
import React from 'react';
import { MyComponent } from './MyComponent';

afterEach(() => {
  jest.useRealTimers();
});

describe('MyComponent', () => {

  jest.useFakeTimers('legacy');

  test('example test case 1', () => {
    jest.useFakeTimers('legacy');
    const { getByText } = render(<MyComponent />);
    expect(getByText('Example 1')).toBeDefined();
  });

  test('example test case 2', () => {
    jest.useFakeTimers('legacy');
    const { getByText } = render(<MyComponent />);
    expect(getByText('Example 2')).toBeDefined();
  });
});

// jest.config.js
module.exports = {
    // ...
    testTimeout: 20000
}

It seems to solve the problem.

Have you tested this? We tried it and it didn’t work.

Same for my react native project. The only thing working for me is the jest.useFakeTimers('legacy')

@akauppi I have the same problem since we updated to Jest 27. I am on Windows 10 + WSL1 (Ubuntu), if that helps anyone.