question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

jest.setTimeout has no effect

See original GitHub issue

Describe the bug

When using jest.setTimeout() in a long-running async test, this has no effect in create-react-app. When running the exact same test once in create-react-app, and once only using jest directly, the following error is thrown with create-react-app:

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

With jest alone, the test passes.

This seems to be related to changes from 3.4.4 to 4.0.0, since when running the same test against react-scripts version 3.4.4, the test also passes.

Did you try recovering your dependencies?

Yes

Which terms did you search for in User Guide?

timeout, setTimeout => no results

Environment

Environment Info:

  current version of create-react-app: 4.0.0
  running from /Users/fm/.npm/_npx/61035/lib/node_modules/create-react-app

  System:
    OS: macOS 10.15.7
    CPU: (8) x64 Intel(R) Core(TM) i7-8559U CPU @ 2.70GHz
  Binaries:
    Node: 12.19.0 - ~/.nvm/versions/node/v12.19.0/bin/node
    Yarn: 1.22.5 - ~/.yarn/bin/yarn
    npm: 6.14.8 - ~/.nvm/versions/node/v12.19.0/bin/npm
  Browsers:
    Chrome: 86.0.4240.198
    Edge: Not Found
    Firefox: 81.0.2
    Safari: 14.0.1
  npmPackages:
    react: 17.0.1 => 17.0.1 
    react-dom: 17.0.1 => 17.0.1 
    react-scripts: 4.0.0 => 4.0.0 
  npmGlobalPackages:
    create-react-app: Not Found

Steps to reproduce

Jest only

  1. Clone repository issue-timeout-jest
  2. npm install
  3. npm run test (passes as expected)

with CRA

  1. Clone repository issue-timeout-cra
  2. npm install
  3. npm run test

Expected behavior

Test should pass:

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total

Actual behavior

Test fails, the following error is thrown:

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

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total

Reproducible demo

issue-timeout-jest: https://github.com/martinfrancois/issue-timeout-jest issue-timeout-cra: https://github.com/martinfrancois/issue-timeout-cra

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:11
  • Comments:6

github_iconTop GitHub Comments

22reactions
ribeaudcommented, Jan 17, 2021

Possible workarounds here:

  1. Add a third parameter to the it. I.e., it('runs slow', () => {...}, 10000)
  2. Write jest.setTimeout(10000); in a file named src/setupTests.js, as specified here.
7reactions
albert-schillingcommented, Dec 20, 2020

Having the same issue. We do visual regression tests for components via Jest and after upgrading to react scripts 4.0.0 all tests fail, because they regularly take longer than 5 seconds. We definitely need a fix for this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest 27: jest.setTimeout has no effect when used in async test ...
I had the same problem and was able to (temporarily) resolve it by moving the jest.setTimeout(...) to the top level scope, i.e. outside...
Read more >
Jest unit test: setTimeout not firing in async test - Stack Overflow
After going through the JEST timer mocks documentation, it occurred to me that the setTimeout goes into infinite recursion as mentioned in the ......
Read more >
Mocking setTimeout with Jest. | Medium - Marek Rozmus
We could increase timeout for unit tests but it is not a good practice. Usually when test exceeds timeout, it means that it...
Read more >
Fix the "not wrapped in act(...)" warning with Jest fake timers
Because that's happening outside of the React call stack, React doesn't know when it's time to flush all the state updates, run all...
Read more >
Testing Asynchronous Code - Jest
... before it can move on to another test. Jest has several ways to handle this. ... Otherwise, a fulfilled promise would not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found