-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
support for fake clock (new Date()
, Date.now()
...) in vitest timers
#506
Comments
We can just mock date when interval moves in our implementation? ( I think, |
But to be clear I am not opposing using it. I think if community decides, we can easily migrate to it. Maybe start a discussion in discord? |
Maybe just moving the mocked date could be enough. Ideally, these kinds of test would work out of the box: test('new Date()', async() => {
vi.useFakeTimers()
const start = new Date().getTime()
vi.advanceTimersByTime(2000)
expect(new Date().getTime()).toBe(start + 2000)
vi.useRealTimers()
})
test('Date.now()', async() => {
vi.useFakeTimers()
const start = Date.now()
vi.advanceTimersByTime(2000)
expect(Date.now()).toBe(start + 2000)
vi.useRealTimers()
}) |
I feel |
its not just moving the current time, its also about scheduling timers and firing them when they should fire. |
It already does it tho? |
I must've mistaken sinon/fake-timers with sinon. Now that I look at it, I think we can implement jest like methods using sinon/fake-timers inside without gaining much weight. Will make a PR soon. |
Released in v0.2.0 |
@sheremet-va if |
@RPDeshaies you would call const date = new Date(1998, 11, 19)
vi.useFakeTimers()
vi.setSystemTime(date)
expect(Date.now()).toBe(date.valueOf())
vi.useRealTimers() |
* feat(useTransition): support for vectors (vitest-dev#376) * refactor(useTransition): cleaning up (vitest-dev#385) * refactor(useWebWorkerFn): Small doc and type improvements (vitest-dev#382) Co-authored-by: Anthony Fu <[email protected]> * feat: pwa reload prompt * chore: update docs * refactor(useWebWorkerFn): Small doc and type improvements (vitest-dev#382) Co-authored-by: Anthony Fu <[email protected]> * chore: update docs * test: simpilfy tests for useTransition * chore: fix tests * feat(useTransition): support for delayed transitions (vitest-dev#386) * feat(useTransition): support for disabled transitions (vitest-dev#436) * feat!: introduce `controls` option * chore: update * chore: update * refactor(useRafFn): remove depreacted APIs * chore: enabled tests for next branch * fix(useFetch)!: allow setting response type before doing request (vitest-dev#454) Co-authored-by: Anthony Fu <[email protected]> * chore: resolve conflicts * feat(useMediaControls): expose source types (vitest-dev#495) * fix(useMediaControls): Removes tracks that have been inserted in html (vitest-dev#493) * chore: release v4.9.3 * fix(usePermission): tolerate error on FireFox * fix(useDevicesList): treat as premssion granted after getUserMedia * chore: release v4.9.4 * chore: fix typo (vitest-dev#502) * feat(useWebSocket): add immediate option (vitest-dev#503) * feat(useAxios): bring API into line with useFetch (vitest-dev#499) * feat(createEventHook): new function (#497) * chore: release v4.10.0 * fix(useMediaControls): Doesn't rewrite default media properties (vitest-dev#500) * feat(useMediaControls): add error event (vitest-dev#509) * feat(useStorage): optimize event handling logic (vitest-dev#505) * feat(useFetch): add afterFetch option, onFetchResponse, and onFetchError (vitest-dev#506) * feat(useWebWoker): return worker (vitest-dev#507) Co-authored-by: Anthony Fu <[email protected]> * fix: Change `onMediaError` to `onSourceError` (vitest-dev#510) * feat(onClickOutside): default to just pointerDown (vitest-dev#508) Co-authored-by: Anthony Fu <[email protected]> Co-authored-by: sibbng <[email protected]> * chore: update docs * chore: release v4.11.0 * fix(onClickOutside): duplicate code (vitest-dev#519) Co-authored-by: Nurettin Kaya <[email protected]> * feat(createEventHook): added interface (vitest-dev#531) * feat(createEventHook): added interface * added types for EventHookOn, EventHookOff, and EventHook trigger * feat(useStorage): allow custom serializer (vitest-dev#528) * feat(useStorage): allow custom serializer * update test * refactor(useMediaControls): Deprecate options that can simply be set as attributes (vitest-dev#514) * useMediaControls: Add `volumechange` event listener * fix: `mute` returned * feat: Deprecate video options: * `poster` * `autoplay` * `preload` * `loop` * `controls` * `playsinline` * `autoPictureInPicture` * fix: Fix deprecated behaviour in demo * fix: Remove deprecated usage from doc * refactor: More polite messages * fix: Remove `console.warn`s * chore: release v4.11.1 * refactor!: remove deprecated apis * chore: add next tag * chore: release v5.0.0-beta.1 * feat: introduce `components` & `directives` (vitest-dev#486) Co-authored-by: Anthony Fu <[email protected]> * docs: re-organize * chore: fix lint * docs: about components * chore: include directives * chore: release v5.0.0-beta.2 * chore: rollback jest * chore: fix docs build * docs: readme for components * docs: add @vueuse/gesture * chore: ship indexes.json * chore: release v5.0.0-beta.3 * feat(typedef): add return typedefs (vitest-dev#543) (vitest-dev#544) * refactor!: change publish strcture and support submodules, close vitest-dev#469 * chore: cleanup stories.tsx * docs: update docs about submodules * chore: fix docs * chore: release v5.0.0-beta.4 * chore: update lock * chore: release v5.0.0-beta.5 * chore: update deps and extend publish memory * refactor: remove `useDeviceLight` * chore: update * chore: fix tests * chore: release v5.0.0-beta.7 * refactor(useWebSocket)!: change immediate default for 5.0.0 (vitest-dev#545) * feat(useIpcRenderer): new add-one & new functions (vitest-dev#547) Co-authored-by: Anthony Fu <[email protected]> * chore: update deps * chore: release v5.0.0-beta.8 * chore: fix docs build * chore(usePointerSwipe): fix typo (vitest-dev#557) * fix(useAuth): now reqiures the auth instance, close vitest-dev#538 * chore: update deps * docs(biSyncRef): fix console output comment (vitest-dev#555) * docs: removed deprecated value from example (vitest-dev#556) * docs(guidlines): added guidelines (vitest-dev#535) * docs: update guidelines * chore: update guidelines Co-authored-by: Scott Bedard <[email protected]> Co-authored-by: Fabian <[email protected]> Co-authored-by: Ismail Gjevori <[email protected]> Co-authored-by: Alex Kozack <[email protected]> Co-authored-by: Shinigami <[email protected]> Co-authored-by: wheat <[email protected]> Co-authored-by: sibbng <[email protected]> Co-authored-by: JserWang <[email protected]> Co-authored-by: Pig Fang <[email protected]> Co-authored-by: ArcherGu <[email protected]> Co-authored-by: Ilya Komichev <[email protected]> Co-authored-by: Daiki Ojima <[email protected]> Co-authored-by: Manaus <[email protected]>
Clear and concise description of the problem
Jest support for timers has builtin support for clocks: when using
jest.useFakeTimers()
, a fake clock is instantiated. Then calls tojest.advanceTimersByTime()
increase the fake clock time. Application code that usenew Date()
, orDate.now()
, orperformance.now()
and others will get this fake time.Jest leverages
@sinonjs/fake-timers
to do so (in itsmodern
implementation, which is the default nowadays).It would be super handy to have vitest do the same. Currently the API mimicks the one from Jest (which is awesome, don't get me wrong 🤗) but doesn't handle this feature, which makes a test suite migration a bit more difficult, as tests that rely on
setTimeout
orsetInterval
will behave the same in vitest and Jest, but the one that leverageDate
will not.Suggested solution
It could be possible to do the same as Jest and directly leverage
@sinonjs/fake-timers
.Alternative
It is possible for a developer who wants to have a fake clock in its test to use
@sinonjs/fake-timers
in addition to vitestAdditional context
No response
Validations
The text was updated successfully, but these errors were encountered: