-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Update to sinon/fake-timers v15 and add setTickMode to timer controls #8726
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
base: main
Are you sure you want to change the base?
Conversation
✅ Deploy Preview for vitest-dev ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
9475530 to
4e552d4
Compare
|
This looks good to me, but I am a bit confused why can't we set the tick in options? Why do we need to call two different functions? Instead of |
|
Thanks for your feedback.
I think this is maybe a "yes and" answer. I think both are useful, e.g. having a default tick mode and wanting to change it for a particular test or within a test. Also, for what it's worth, you could compress the first example since they return the utils: The difficulty with adding this to the config object is that there is already a config property (actually two of them) for the |
|
Good job! Thanks for this @atscott. Pedagogically speaking, I am afraid that Andrew, is there a reason to name the option @sheremet-va do you really want the I am afraid of the combinatorial complexity. e.g. |
This follows the pattern used with every other API that touches the clock. More specifically, it is a parallel to
I don't disagree entirely but I have also described above how this is more a limitation of the current 1-1 passthrough to sinon options and that
Edit to the above: Apologies, I was misreading the thread. I understand now that this was a discussion about how to expand the configuration to allow defining this new behavior in the initial config. And yes, I agree that the current configuration isn't quite suited for it. |
Ok. It just seemed to me that it only applies to
My bad, my last example was not very clear.
🙌 |
yea, and I think that’s generally true. The reason I felt like changing it here is that the option is trying to reflect the API name it’s conceptually wrapping, which for Vi is advanceTimersToNextTimerAsync. I don’t think either name is great for understandability, but in the absence of greatness, we can at least opt for being literal 🤷♂️ |
4e552d4 to
a832dce
Compare
This change exposes the `setTickMode` function from the underlying `fake-timers` library.
This is to align with the new feature introduced in `fake-timers`.
The new `setTickMode` method allows developers to configure whether time should auto advance and what the delta should be after the clock has been created. Prior to this, it could only be done at creation time with `shouldAdvanceTime: true`.
This also adds a new mode for automatically advancing time that moves more quickly than the existing shouldAdvanceTime, which uses real time.
Testing with mock clocks can often turn into a real struggle when dealing with situations where some work in the test is truly async and other work is captured by the mock clock. When using mock clocks, testers are always forced to write tests with intimate knowledge of when the mock clock needs to be ticked. It is ideal for test code to be written in a way that is independent of whether a mock clock is installed.
`shouldAdvanceTime` is essentially `setInterval(() => clock.tick(ms), ms)` while this feature is `const loop = () => setTimeout(() => clock.nextAsync().then(() => loop()), 0);`
There are two key differences:
1. `shouldAdvanceTime` uses `clock.tick(ms)` so it synchronously runs all timers inside the "ms" of the clock queue. This doesn't allow the microtask queue to empty between the macrotask timers in the clock.
2. `shouldAdvanceTime` uses real time to advance the same amount of real time in the mock clock. `setTickMode({mode: "nextAsync"})` advances time as quickly possible and as far as necessary.
See: sinonjs/fake-timers@108efae
a832dce to
13a3046
Compare
|
@sheremet-va Do you have any opinions on the API naming/shape? Happy to rebase this again but will otherwise leave it alone if there's still some bike-shedding discussions to work through. |
I am fine with the naming, we are just waiting for 4.1 beta (in ~2 weeks). |
Description
This change exposes the
setTickModefunction from the underlyingfake-timerslibrary.This is to align with the new feature introduced in
fake-timers.The new
setTickModemethod allows developers to configure whether time should auto advance and what the delta should be after the clock has been created. Prior to this, it could only be done at creation time withshouldAdvanceTime: true.This also adds a new mode for automatically advancing time that moves more quickly than the existing shouldAdvanceTime, which uses real time.
Testing with mock clocks can often turn into a real struggle when dealing with situations where some work in the test is truly async and other work is captured by the mock clock. When using mock clocks, testers are always forced to write tests with intimate knowledge of when the mock clock needs to be ticked. It is ideal for test code to be written in a way that is independent of whether a mock clock is installed.
shouldAdvanceTimeis essentiallysetInterval(() => clock.tick(ms), ms)while this feature isconst loop = () => setTimeout(() => clock.nextAsync().then(() => loop()), 0);There are two key differences:
shouldAdvanceTimeusesclock.tick(ms)so it synchronously runs all timers inside the "ms" of the clock queue. This doesn't allow the microtask queue to empty between the macrotask timers in the clock.shouldAdvanceTimeuses real time to advance the same amount of real time in the mock clock.setTickMode({mode: "nextAsync"})advances time as quickly possible and as far as necessary.See: sinonjs/fake-timers@108efae
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
pnpm-lock.yamlunless you introduce a new test example.Tests
pnpm test:ci.Documentation
pnpm run docscommand.Changesets
feat:,fix:,perf:,docs:, orchore:.