-
Notifications
You must be signed in to change notification settings - Fork 470
Limit use of realTimers to Jest's fakeTimers #652
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
Limit use of realTimers to Jest's fakeTimers #652
Conversation
A recent change to `runWithRealTimers` added a check for the `clock` attribute on `setTimeout` so that we can determine whether timers were faked with Jest's modern fake timers (which are based on `@sinonjs/fake-timers`). Unfortunately, this attribute can be present when users are not using Jest's fake timers and are using sinonjs. Also, when `useFakeTimers` are invoked without parameters in Jest 26, the legacy timers are used by default. This PR addresses two issues: 1. Don't use realTimers if the timers were not faked by Jest. 2. If we're using realTimers, make sure we're explicit when calling `useFakeTimers` so that the fakes are restored to their original state.
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit f86b41e:
|
Codecov Report
@@ Coverage Diff @@
## master #652 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 24 24
Lines 575 582 +7
Branches 145 149 +4
=========================================
+ Hits 575 582 +7
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Solid. Thank you!
🎉 This PR is included in version 7.16.2 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
Thanks @kentcdodds, feels good to have contributed. |
@all-contributors please add @kgregory for code and tests |
I've put up a pull request to add @kgregory! 🎉 |
Thanks so much for your help! I've added you as a collaborator on the project. Please make sure that you review the |
What:
Modify
runWithRealTimers
so that real timers are only used if they were faked by Jest's legacy or modern implementation of fake timers.Also, when restoring Jest's fakes, be explicit in specifying whether we're restoring the
modern
orlegacy
fakes.Why:
A recent change to
runWithRealTimers
added a check for theclock
attribute onsetTimeout
so that we can determine whether timers were faked with Jest's modern fake timers (which are based on@sinonjs/fake-timers
). See issue thread.Unfortunately, this attribute can be present when users are not using Jest's fake timers and are using sinonjs. Also, when
useFakeTimers
are invoked without parameters in Jest 26, the legacy timers are used by default.How:
Determine if Jest legacy timers are used by checking
globalObj.setTimeout._isMockFunction
.Determine if Jest modern timers are used by checking for the presence of
globalObj.setTimeout.clock
and also by verifying that use ofjest.getRealSystemTime
is supported. It was introduced in Jest 26 and is only available when modern fake timers are used.Checklist:
docs site