feat!: enable mocking Temporal without fake timers#10757
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. |
hi-ogawa
left a comment
There was a problem hiding this comment.
I'm not sure we like to bring another own custom "mock date" machinery like packages/vitest/src/integrations/mock/temporal.ts.
I'm wondering if we can drop our mock/date.ts too now and go all in with fake timer. Basically making setSystemDate to be a sugar of something like:
// setSystemDate(date)
// =>
vi.useFakeTimers({
toFake: ['Date', 'Temporal'],
now: date
})Probably we still keep a internal state matrix of _fakingTime x _fakingDate but switch to the above sugar for _fakingTime = false, _fakingDate = truthy scenario.
vitest/packages/vitest/src/integrations/mock/timers.ts
Lines 21 to 28 in ad065a7
Such change doesn't look much breaking.
|
I updated the code in line with the suggestion. |
Co-authored-by: OpenCode <noreply@opencode.ai>
hi-ogawa
left a comment
There was a problem hiding this comment.
Thanks for follow-up!
Regarding the breaking change aspect of the sugar idea, on the contrary to what I thought, it looks like this is effectively zero breaking change and basically pure refactoring to borrow fake timer Date implementation.
What I was wrongly thinking is that, the change may surface bare vi.setSystemDate would make vi.isFakeTimers = true and thus it may kicks in expect.poll/vi.waitFor/until's fake timer auto advances, but actually we still keep internal states _fakingTime/_fakingDate so isFakeTimers is still false. So, this still hold true:
vi.setsystemtime(0)
await vi.waitFor(() => sleep(100))
console.log(Date.now()) // => 0 (doesn't auto advance still)|
Note that we no longer need to keep |
Description
Resolves #10345
#10654 added support for mocking Temporal with
useFakeTimers, butsetSystemDatewithout fake timers still mocked onlyDate(it's a nice-to-have feature, cf. #10654 (comment)).This pull request makes
setSystemDatemockTemporal.Nowif it is available on the global namespace, in addition toDate.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:.