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 has changed its timer implementation causing waitFor() not to work properly

See original GitHub issue
  • @testing-library/dom version: 7.5.2
  • Testing Framework and version: 10.0.4
  • DOM Environment: Jest Jest version: 25.1.0 Node v12

We had some code which uses fakeTimers, I noticed it was screwing up waitFor(). So i needed to run jest.useRealTimers() just before calling waitFor() to fix the issue.

This shouldn’t be needed, as testing-library tries to safe-guard against fakeTimer use, you can see here: https://github.com/testing-library/dom-testing-library/blob/master/src/helpers.js#L5-L8

However, i noticed this line will always return undefined regardless of whether you have fakeTimers on or not. This is due to the fact that globalObj.setTimeout._isMockFunction is always undefined. My guess is Jest used to mock setTimeout using its own utilities and this property once existed, but now Jest uses @Sinon/MockTimers where this property is not added.

When did this happen?

Looks like the change happened in Jest v26 https://github.com/facebook/jest/releases/tag/v26.0.0

there were also implementation changes in v25.1 https://github.com/facebook/jest/releases?after=v25.5.1

_isMockFunction is added here: https://github.com/facebook/jest/blob/master/packages/jest-fake-timers/src/legacyFakeTimers.ts#L370

It was swapped out in v25, plan here: https://github.com/facebook/jest/pull/7776#issuecomment-513552169

Solution

The fix is to find another way to see if the timers are being mocked or not, as the current heuristics don’t work.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
kgregorycommented, Jun 17, 2020

I’d love to

3reactions
kentcdoddscommented, Jun 15, 2020

🎉 This issue has been resolved in version 7.15.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Testing Library's waitFor not working - Stack Overflow
Thus I want to change the default wait time for waitFor , but I can't find a way to do it from the...
Read more >
Timer Mocks - Jest
This is replacing the original implementation of setTimeout() and other timer functions. Timers can be restored to their normal behavior with ...
Read more >
Testing asynchronous behaviour in React - Moxio
We use jest as a test runner and assert for assertions. The code examples use typescript , but hopefully nothing too scary. A...
Read more >
Common mistakes with React Testing Library - Kent C. Dodds
Advice: use find* any time you want to query for something that may not be available right away. Passing an empty callback to...
Read more >
Frontend testing standards and style guidelines - GitLab Docs
Most of the time, you should use RSpec for your feature tests. ... be to not test library internals, but expect that the...
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