Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exceeded timeout of 5000 ms for a test #11607

Closed
psxpa3 opened this issue Jun 23, 2021 · 55 comments
Closed

Exceeded timeout of 5000 ms for a test #11607

psxpa3 opened this issue Jun 23, 2021 · 55 comments
Labels
Bug Report Needs Reproduction Every bug report requires a minimal reproduction Needs Triage

Comments

@psxpa3
Copy link

psxpa3 commented Jun 23, 2021

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?

@cBiscuitSurprise
Copy link

I've created a repro for my case: https://github.com/cBiscuitSurprise/jest-setTimeout-11607-repro

@hdorgeval
Copy link

I have the same error on my repo: playwright-fluent.
Current Jest version is 26.6.3 and all my tests have this:

beforeEach((): void => {
    jest.setTimeout(60000);
    p = new SUT.PlaywrightFluent();
  });

When upgrading to v27.0.5, every test now fails with the error message:

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

To reproduce the problem:

  • clone the repo

  • npm i

  • npm install --save-dev jest@latest ts-jest@latest

  • npm run install-peers

  • now open this test file in VSCode: src/fluent-api/tests/on-request-to-respond-with/on-request-to-respond-with.chromium.post.test.ts

  • in the debug pane, launch the jest-current-file

Thanks

@akauppi
Copy link

akauppi commented Jul 7, 2021

@psxpa3 @hdorgeval Are you guys on Windows 10 + WSL2 (Ubuntu)?

I started to see these on that system, a while back, but not on macOS or Alpine Linux.

—-

Edit: Got my timeout problems resolved. Were likely unrelated to things reported here.

@marcelgerber
Copy link

marcelgerber commented Jul 7, 2021

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

@hdorgeval
Copy link

hdorgeval commented Jul 7, 2021

Hi @akauppi , I have the problem on my Mac (Big Sur 11.4), but also when running the tests on Github CI/linux and on Appveyor.

So I have the same problem on all platforms.

It seems to me that this problem only happens to those who are using jest with an e2e testing framework, because in this situation tests are likely to run in more than 5 seconds.

In my case, tests run on an average of 30s, but it can take up to several minutes.

@vdanylov
Copy link

vdanylov commented Jul 9, 2021

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

@hdorgeval
Copy link

Hi @vdanchenkov, unfortunately your workaround did not work in my case.

@mkermani144
Copy link

Same problem on linux.

@phil-lgr
Copy link

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

@phil-lgr
Copy link

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?

@hdorgeval
Copy link

Hi @phil-lgr , I tried your work-around on my repo playwright-fluent.

Unfortunately it does not work for me.

The main difference with your code is that all my tests have a beforeEach/afterEach and for some a BeforeAll/AfterAll.

@GGrassiant
Copy link

Same as @hdorgeval on my end.
I tried to switch from jest.useFakeTimers('legacy') to jest.useRealTimers(); but it would not work.
Worth mentioning I do not have callbacks such as beforeEach/afterEach or BeforeAll/AfterAll on my test boilerplate

@izhaoqing
Copy link

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

It seems to solve the problem.

@JonWallsten
Copy link
Contributor

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

@izhaoqing
Copy link

@JonWallsten I have tested that. Setting testTimeout solved the problem I encountered.

@GGrassiant
Copy link

GGrassiant commented Aug 20, 2021

// 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')

@hdorgeval
Copy link

Hi @izhaoqing , thank you for your feedback! 👍

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

This solved the problem for me, and unlike others in this issue, this is the only working solution for my use case (playwright-fluent).

@wbsdickson
Copy link

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

@marklanhamhc
Copy link

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();
  });
});

@alapanerao
Copy link

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

This one worked for me. Thanks!

@eschneor
Copy link

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

It seems to solve the problem.

This solved the issue for me. What that option does? Is v27 broken?

@axelboc
Copy link

axelboc commented Sep 17, 2021

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!

@pereozogu
Copy link

Used @wbsdickson suggestions and it worked for me. Thank you so much.
Before, it failed at [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."]

@filipe-costa
Copy link

Unfortunately, I have tried the solutions mentioned above but my tests keep timing out, even tho I have been able to log both in the terminal console within afterEach callback and on the browser, both of them are being populated correctly. Despite increasing the timeout to 30 or 60s it will always timeout.

My test environment uses JSDom, my API is promise based and I use Fetch API which I am mocking using:

global.fetch = jest.fn()
  .mockImplementation(() => {
    return Promise.resolve({json: () => mockData})
  })

Where mockData is a JSON file.

My dependencies are the following:

{
  "jest": "^27.2.5",
  "ts-jest": "^27.0.5",
  "typescript": "^4.4.3"
}

My node and npm versions:

node v16.9.1 (npm v7.24.0)

My operating system:

macOS Big Sur v11.6.

@dev1ninja
Copy link

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

This did work for me.
It was really helpful in my case.
Thank you.

@fialhoFabio
Copy link

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

same for me, my solution was put write jest.setTimeout(30000); in the jest setup file.

@Carlosajunior
Copy link

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?

Your solution worked for me, thank you

@Akifcan
Copy link

Akifcan commented Jan 13, 2022

I'm not 100% sure but I faced this error after run
npm run test:ci
in nest.js

@julianYaman
Copy link

julianYaman commented Jan 13, 2022

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

Thank you very much guys

@imrohitsinghal
Copy link

imrohitsinghal commented Feb 2, 2022

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.

@OdapX
Copy link

OdapX commented Feb 3, 2022

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

code

@kenchambers
Copy link

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

this is the only one that worked for me

"devDependencies": {
    "babel-jest": "^27.3.1",
    "jest": "^27.3.1"
  },

@leepowellnbs
Copy link

Same issue, jest.useFakeTimers('legacy') is working for now.

@shawn-eary
Copy link

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!

I can't comment right now about the Win 10 stuff, but for me, using testTimeout in my package.json file got me past this bizarre issue. I'm trying to run an async Selenium test inside Jest. Not sure if this SO post is related or not...
https://stackoverflow.com/questions/52633843/jest-unit-test-settimeout-not-firing-in-async-test

@Christopher2K
Copy link

Same issue, jest.useFakeTimers('legacy') is working for now.

This worked for me. I'm running jest 27.5.1 in a react-native environment

@maxkrumpe
Copy link

maxkrumpe commented Apr 6, 2022

For me the problem is solved when I use the flag --runInBand.

With the command react-scripts test some tests will fail with the message "Exceeded timeout of 5000 ms for a test".
With the command react-scripts test --runInBand no tests are failing.

@SimenB SimenB added the Needs Reproduction Every bug report requires a minimal reproduction label Apr 6, 2022
@github-actions
Copy link

github-actions bot commented Apr 6, 2022

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.

@github-actions github-actions bot closed this as completed Apr 6, 2022
@cBiscuitSurprise
Copy link

@SimenB did you not see this (first comment posted in June of 2021)? #11607 (comment)

@KVPasupuleti
Copy link

I need the timeout inside a test.

And I am using create-react-app with Craco.

Any solutions for this?

@troyblank
Copy link

To get my test back to working after upgrading I did two things

  1. used the "legacy" param > jest.useFakeTimers( 'legacy' ) (thanks to vdanylov)
  2. at the end of my test I put this line jest.runOnlyPendingTimers() (which for some reason tells the test it's time to move on and not hold on to old timers, maybe???)

@AnisBelahcene
Copy link

AnisBelahcene commented Apr 11, 2022

Actually i am having the same problem and i tried everything it not work someone knows an other solution ? i dont use any timer the bug is when i try to test my login route

@PabloLION
Copy link

PabloLION commented Apr 13, 2022

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?

This comment works for me, with jest 27, ts-jest 27 and node 17. I'm trying to make this the last post in this issue by summarize up all problems.

@SMAsadAli
Copy link

I have solved most of my problems by increasing timeout or using faketimers. But i am getting error on this one which i am unable to resolve, any help would be appreciated.

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

> 1 | export * from '@testing-library/react-native';

@mikansc
Copy link

mikansc commented May 5, 2022

I was extracting "done" as argument from my callback function:

  it('throws an error if the email is already in use', async (done) => {
    expect.assertions(1);

    fakeUsersService.find = () => Promise.resolve([{ email: 'in-use@email.com' } as User]);
    await expect(service.signup('in-use@email.com', '123456')).rejects.toThrow(BadRequestException);
  });

Removing the done argument from the callback solved my problem.

@PabloLION
Copy link

I have solved most of my problems by increasing timeout or using faketimers. But i am getting error on this one which i am unable to resolve, any help would be appreciated.

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

> 1 | export * from '@testing-library/react-native';

@AsadShah1995 have you tried with This comment? It works for me, with jest 27, ts-jest 27 and node 17. I'm trying to make this the last post in this issue by summarize up all problems.

@github-actions
Copy link

github-actions bot commented Jun 5, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug Report Needs Reproduction Every bug report requires a minimal reproduction Needs Triage
Projects
None yet
Development

No branches or pull requests