-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
[Feature]: Allow mocking the Date without mocking timers #12452
Comments
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
Not stale, a PR was opened 7 days ago |
@glenjamin In case if you would like to simply mock the value returned by jest.spyOn(Date, 'now').mockImplementation(() => 123456); The thing is that fake timers are sort of freezing the time. The clock has to be advanced manually, code can hang because of this. The mocked value works in some cases, but that is not a replacement for a ticking clock. Another possible solution (part of the same #12572, hence not yet working) could be: jest.useFakeTimers({advanceTimers: true, now: new Date('2015-09-25')}); |
Yep, in my case I was also using I ended up doing this while I wait for #12572 const frozen = new Date('2022-02-21T17:35:00Z');
jest.spyOn(global, 'Date').mockReturnValue(frozen);
jest.spyOn(Date, 'now').mockReturnValue(frozen.valueOf()); |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
🚀 Feature Proposal
Currently
jest.useFakeTimers()
enables mocking of all time and timer related functions.I'd like to propose allowing callers to be more selective about what timer functions are actually mocked
This could be a case of exposing the
toFake
option from @sinonjs/fake-timers, or it could take the form of a jest-specific API.Motivation
I've run into a case where I'm doing a somewhat integrationy test, but I want to control the current time to make my assertion simpler, however, when I do
Then my code hangs, because somewhere in the depths of the stuff that's running, there must be a timer somewhere.
Example
No response
Pitch
Currently timer mocking is part of the jest core API by wrapping sinon, but there's no way to control the options passed to sinon.
The text was updated successfully, but these errors were encountered: