Using Jest mock timers and waitFor together causes tests to timeout
See original GitHub issueDescribe the bug
Related to #391. Using jest.useFakeTimers()
in combination with waitFor
, causes the tests using waitFor
to fail due to timeout error:
Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Error: Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.
Relying upon jest.useFakeTimers("modern")
instead causes the above failure for all tests if the file merely imports waitFor
at all, regardless if the given test uses waitFor
or not.
Expected behavior
All tests in the reproduction test case should pass.
Steps to Reproduce
- Clone the reproduction test case.
yarn install
yarn test
Versions
npmPackages:
@testing-library/react-native: ^7.0.1 => 7.0.1
react: ^16.13.1 => 16.13.1
react-native: ^0.63.2 => 0.63.2
react-test-renderer: ^16.13.1 => 16.13.1
Issue Analytics
- State:
- Created 3 years ago
- Reactions:16
- Comments:16 (16 by maintainers)
Top Results From Across the Web
Timer Mocks - Jest
In the following example we enable fake timers by calling jest.useFakeTimers() . This is replacing the original implementation of setTimeout() ...
Read more >Using Fake Timers | Testing Library
When using fake timers in your tests, all of the code inside your test uses fake timers. The common pattern to setup fake...
Read more >Test functionality with timeouts in react-testing-library and jest
An obvious reason would be that you do not want to wait 5 seconds in your test to then continue. Imagine a component...
Read more >Common mistakes with React Testing Library - Kent C. Dodds
The purpose of waitFor is to allow you to wait for a specific thing to happen. If you pass an empty callback it...
Read more >Testing Asynchronous Behavior - Vue Test Utils
This example uses Jest to run the test and to mock the HTTP library axios . More about Jest manual mocks can be...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Not sure if I understood your issues correctly. AFAIK when using fake timers you should not use call
waitFor
withawait
.Please compare how were are using fake timers with
waitFor
in our own test suit.It seems that just this change (
await waitFor(() => {
->waitFor(() => {
) fixes yourlegacy-timers.test.js
.Not sure how to fix your failing tests using modern timers.
Okay it looks like the general approach followed by wait-for-expect to capture the global timer funcs before they get mocked works, but it has highlighted a problem with the ‘modern’ timer mocks which is caused partially by the
'react-native'
preset polyfillingglobal.promise
and partially by the new timer mocks mockingprocess.nextTick
. I’ve written most of the code for the first bit but to make it work with modern timers we need to patch a line in ‘@jest/fake-timers’.Will send a PR tomorrow.