-
-
Notifications
You must be signed in to change notification settings - Fork 34.2k
test_runner: Add Date to the supported mock APIs #48638
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
test_runner: Add Date to the supported mock APIs #48638
Conversation
|
Review requested:
|
|
I don't think Date.now should be passed as a param while enabling timers as it's not a timer. IMHO In this first version Date.now should advance in time when some .tick call happens cc @nodejs/test_runner |
I think it's a timer in the sense that's an API to manipulate or count time in general. But I can see your point too, it can be confusing, I took as an inspiration the last PR and Sinon's fake timers API, this last one also mocks not only the But IMO it kinda makes sense to be here, I would expect an API to manipulate date to be in the fake timers implementation as an user.
Regarding this, it's already happening in this implementation, tick is advancing the |
|
Mocking |
Yeah I thought so later on, it wouldn't make a lot of sense 😄 thanks for clarifying! Will add this to the next steps |
|
a83b9f9 introduces a problem and a question. When From here I think we have two options:
Any thoughts? |
| this.#now = 0; | ||
| } else if (this.#isValidDateWithGetTime(args[0])) { | ||
| // First argument is the initial time as Date | ||
| this.#now = args[0].getTime(); |
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.
do we mean to call .getTime() on the argument, or should this instead be:
| this.#now = args[0].getTime(); | |
| this.#now = DatePrototypeGetTime(args[0]); |
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's the intended one, if the first argument is an instance of Date then we will call the getTime method on it to get the timestamp
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.
right but if it's an actual Date object - which may not be instanceof Date - you don't want to rely on the presence of Date.prototype.getTime at runtime.
| #isEnabled = false; | ||
| #currentTimer = 1; | ||
| #now = DateNow(); | ||
| #now = 0; |
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.
#now should be DateNow unless it's changed by .setTime
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.
But that differs from the implementation from fake-timers. If the intention is to be close to that API then we wouldn't be able to start it as DateNow, however, I'm not against that too. I think it's worth a discussion with @nodejs/test_runner
On my side I think the pros is that you can start the Date object without actually setting anything on it, as opposed to having to set the time at any given new fake timer instance because otherwise you'd have Jan 1, 1970. Which also can be a pro to some people as it's more explicit to what time is the initial time.
| p.then(common.mustCall((result) => { | ||
| assert.ok(result); | ||
| })); | ||
| p.then( |
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
I think runAll shouldn't affect Date.now as it's just releasing all pending operations so option 1 is the best IMHO |
There is a third option here which is to have a property in the |
|
I don't understand the rationale not to progress the clock date whenever it should happen (e.g by 20ms if a 20ms setTimeout delayed has run)? I think this was the sinon/jest behavior and I don't think anyone ever asked for anything different? |
|
I was under the impression that when Date was mocked, time never advanced except manually by the user. |
As far as I remember from our code in fake-timers we always progress Date/performance.now when we progress timers. |
Alright! I think I'll work on them today up to the end and we can implement that version, I also think it's the best solution, then we can have a base v1 ready :) |
|
@ljharb @ErickWendel @benjamingr I think I'm done with this version 😄 if you could please review it 🚀 |
|
Other than my comments LGTM |
|
Landed in 45a0b15 |
|
OMG FINALLY ⭐ |
|
Thanks @anonrig for the flaky PRs! |
This builds on top of @ErickWendel's #47775, I saw the next steps would be to implement the mock timers for Date.now (and thus, the Date object) and performance.now.
This PR implements the Date.now mock, I'll also work on performance.now on another PR to make it simpler to review. This one includes the Docs already updated and the added tests.
This heavily builds on Sinon's Fake Timers for the base edge cases
To-Do
setTimeactually call thetickmethod and pass the timeDate, or a specific number to make it's implementation be the same as fake-timersenableto accept multiple overloadsNext iterations
performance.nowprocess.hrtimeNew
MockTimersAPIIt's also possible to omit the initial parameter and pass on only the initial epoch, which will enable all timers with that epoch set:
Lastly, you can omit all parameters to enable all timers at the epoch 0:
Example usage
All the remaining APIs are the same