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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide a way to find out whether fake timers have been enabled. #10555
Comments
I think something similar can be achieved by doing the following: afterEach(() => {
if (jest.isMockFunction(setTimeout)) {
jest.runOnlyPendingTimers();
jest.useRealTimers();
}
}); |
Thanks for the suggestion @cschwebk ! That seems like a very clean workaround, but it'd be awesome if it was officially supported/documented, since strictly speaking without that, it's not guaranteed that enabling fake timers will make |
It's worth noting that these methods aren't actually correct. If you use I was not able to find any way to differentiate between a spy and a mock so I'm not sure how it'd be possible to determine this reliably, even in a hacky way. If only there was a property/method exposed by Jest which could help us! 馃槈 |
This seems not to work with jest 28.1.0 - As a temporary and hacky workaround that is almost certain to break, checking the I can't explain why this is the case (maybe some setup from jest-environment-jsdom?), as To maybe provide some reasoning for why this feature is useful, it can be used with the react-testing-library user-event companion library. Setup of this is something like: userEvent.setup(); But when using mocked timers, you must provide an userEvent.setup({ advanceTimers: jest.advanceTimersByTime }); To simplify this, it is often put in a setup wrapper, so should be able to dynamically set the correct |
I can confirm that if (typeof jest !== 'undefined' && setTimeout.clock != null && typeof setTimeout.clock.Date === 'function') {
// ...
} Can you please consider deprecating |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
Not stale |
declare global {
interface DateConstructor {
/* Jest uses @sinonjs/fake-timers, that add this flag */
isFake: boolean;
}
}
const hasFakeTimers = global.Date.isFake === true; |
馃殌 Feature Proposal
Provide a method (e.g.
jest.usingFakeTimers()
) that returnstrue
ifjest.useFakeTimers
was previously invoked. Alternatively, a property or a config object could be exposed with the same information.Motivation
It is often convenient to have a global cleanup routine for tests that does things like always reverting to real timers, etc. It would be awesome if we could flush timers (e.g.
jest.runOnlyPendingTimers()
) in this cleanup routine as well, but to do that, one would need to find out if fake timers are actually being used.There are already examples of libraries resorting to introspection of the
setTimeout
implementation to try to deduce this for similar purposes.@kentcdodds had to do the following https://github.com/testing-library/react-testing-library/pull/720/files recently
Would be awesome if
jest
exposed an official method to check the above, so that tooling wouldn't break if the聽underlying implementation were to change.Example
Pitch
Why does this feature belong in the Jest core platform?
Since
jest.useFakeTimers
is a part of the core platform, it seems reasonable to be able to find out whether that call had previously been invoked. The core platform is the only place that would be able to provide this information.The text was updated successfully, but these errors were encountered: