Skip to content
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

[Feature]: Allow mocking the Date without mocking timers #12452

Closed
glenjamin opened this issue Feb 21, 2022 · 6 comments 路 Fixed by #12572
Closed

[Feature]: Allow mocking the Date without mocking timers #12452

glenjamin opened this issue Feb 21, 2022 · 6 comments 路 Fixed by #12572

Comments

@glenjamin
Copy link

馃殌 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

      jest.useFakeTimers();
      jest.setSystemTime(new Date('2022-02-21T17:35:00Z'));

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.

@github-actions
Copy link

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.

@github-actions github-actions bot added the Stale label Mar 23, 2022
@glenjamin
Copy link
Author

Not stale, a PR was opened 7 days ago

@github-actions github-actions bot removed the Stale label Mar 23, 2022
@mrazauskas
Copy link
Contributor

@glenjamin In case if you would like to simply mock the value returned by Date.now(), all you need is:

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')});

@glenjamin
Copy link
Author

Yep, in my case I was also using new Date(), so mocking now was insufficient.

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());

@SimenB
Copy link
Member

SimenB commented Apr 5, 2022

@github-actions
Copy link

github-actions bot commented May 6, 2022

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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
3 participants