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.

Using Jest mock timers and waitFor together causes tests to timeout

See original GitHub issue

Describe 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

  1. Clone the reproduction test case.
  2. yarn install
  3. 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:closed
  • Created 3 years ago
  • Reactions:16
  • Comments:16 (16 by maintainers)

github_iconTop GitHub Comments

5reactions
mdjastrzebskicommented, Sep 8, 2020

Not sure if I understood your issues correctly. AFAIK when using fake timers you should not use call waitFor with await.

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 your legacy-timers.test.js.

Not sure how to fix your failing tests using modern timers.

2reactions
mikeduminycommented, Oct 7, 2020

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 polyfilling global.promise and partially by the new timer mocks mocking process.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.

Read more comments on GitHub >

github_iconTop 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 >

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