-
-
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
setSystemTime and getRealSystemTime type and doc update #10169
Conversation
I wasn't sure if I'm happy to do any cleanup necessary on changes in this PR that aren't desirable. Also, thanks for these new functions! |
|
||
expect(Date.now()).toBe(0); | ||
|
||
jest.setSystemTime(1000); |
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.
this doesn't use a Date
, but a number like above
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.
🤦 it does indeed, thank you.
|
||
expect(Date.now()).toBe(0); | ||
|
||
jest.setSystemTime(1000); |
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.
same here
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.
Thanks! I think this only needs a tweak to the tests and it's good to go 👍
@SimenB thanks for you review, I've pushed an update to those tests! |
@@ -647,13 +647,13 @@ This means, if any timers have been scheduled (but have not yet executed), they | |||
|
|||
Returns the number of fake timers still left to run. | |||
|
|||
### `.jest.setSystemTime()` | |||
### `jest.setSystemTime(now?: number | Date)` |
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.
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.
I think I have (or at least, I intended to) here: https://github.com/facebook/jest/pull/10169/files#diff-82d75bb4a1a86113540ea33c2bd6f24d
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.
Oh, seems I'm blind 😅 Thanks!
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Summary
This PR does two (related) things.
Update
setSystemTime
now
param types to allowDate
The internal types for
setSystemTime
are currentlynow?: number
, but in using jest I found passing aDate
also worked and was a bit more ergonomic in some cases. For example:The types for the underlying
this._clock.setSystemTime
call injest-fake-timers/src/modernFakeTimers.ts
supportDate
, so echoing that insetSystemTime
seemed reasonable. I was looking into this while working on a PR to add types forsetSystemTime
andgetRealSystemTime
to DefinitelyTyped, and wanted to make sure that those types matched jest's codebase's types and the intended usage.Update
setSystemTime
andgetRealSystemTime
docssetSystemTime
did not directly document its params, so they took me as a user a bit of time to figure out. I wasn't sure what the format for these should be.advanceTimersToNextTimer
just hasjest.advanceTimersToNextTimer(steps)
(no types), whereasuseFakeTimers
hasjest.useFakeTimers(implementation?: 'modern' | 'legacy')
. I erred on the side of adding a type to the param, the union members are native and I think the types would be useful to both js and ts folks.setSystemTime
andgetRealSystemTime
both have a preceding.
char, which I removed. I'm not sure if this was intentional, but the other functions/headers in that doc do not have one and I didn't see any reference to them being experimental or anything (aside from requiring the "modern" timers).Test plan
For the new type, I added test cases for
setSystemTime(now: Date)
wherever I saw test cases forsetSystemTime(now: number)
. I ran the tests locally (albeit without mercurial installed, which didn't seem to affect these particular files).I took a look at the doc changes to make sure they looked alright. They look like:
Whereas on the live site, they look like: