From 92f9fd2c7fbde7d1b2ee83723dd97bf0d2d62aa3 Mon Sep 17 00:00:00 2001 From: Prajwal2246 <213174436+Prajwal2246@users.noreply.github.com> Date: Wed, 16 Sep 2026 15:20:25 +0530 Subject: [PATCH 01/13] feat: support Temporal.Instant in vi.setSystemTime ref #8107 Co-Authored-By: Claude Opus 5 (1M context) --- docs/api/vi.md | 4 ++-- packages/vitest/src/integrations/mock/timers.ts | 4 ++-- packages/vitest/src/integrations/vi.ts | 4 ++-- test/unit/test/timers-temporal.test.ts | 10 ++++++++++ 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/docs/api/vi.md b/docs/api/vi.md index 5e773eb2c9c4..80c5dcf1cf02 100644 --- a/docs/api/vi.md +++ b/docs/api/vi.md @@ -1163,14 +1163,14 @@ await vi.runOnlyPendingTimersAsync() ### vi.setSystemTime ```ts -function setSystemTime(date: string | number | Date): Vitest +function setSystemTime(date: string | number | Date | Temporal.Instant): Vitest ``` If fake timers are enabled, this method simulates a user changing the system clock (will affect date related API like `hrtime`, `performance.now` or `new Date()`) - however, it will not fire any timers. If fake timers are not enabled, this method will only mock `Date.*` and `Temporal.Now.*` calls. Useful if you need to test anything that depends on the current date - for example [Luxon](https://github.com/moment/luxon/) calls inside your code. -Accepts the same string and number arguments as the `Date`. +Accepts the same string and number arguments as the `Date`, or a `Temporal.Instant`. ```ts const date = new Date(1998, 11, 19) diff --git a/packages/vitest/src/integrations/mock/timers.ts b/packages/vitest/src/integrations/mock/timers.ts index 0689961098ec..ac044371b90a 100644 --- a/packages/vitest/src/integrations/mock/timers.ts +++ b/packages/vitest/src/integrations/mock/timers.ts @@ -200,8 +200,8 @@ export class FakeTimers { } } - setSystemTime(now?: string | number | Date): void { - const date = (typeof now === 'undefined' || now instanceof Date) ? now : new Date(now) + setSystemTime(now?: string | number | Date | { epochMilliseconds: number }): void { + const date = (typeof now === 'undefined' || now instanceof Date) ? now : new Date(typeof now === 'object' ? now.epochMilliseconds : now) if (this._fakingTime) { this._clock.setSystemTime(date) } diff --git a/packages/vitest/src/integrations/vi.ts b/packages/vitest/src/integrations/vi.ts index 19b763e792fb..4691864dc500 100644 --- a/packages/vitest/src/integrations/vi.ts +++ b/packages/vitest/src/integrations/vi.ts @@ -86,7 +86,7 @@ export interface VitestUtils { * If fake timers are enabled, this method simulates a user changing the system clock (will affect date related API like `hrtime`, `performance.now` or `new Date()`) - however, it will not fire any timers. * If fake timers are not enabled, this method will only mock `Date.*` and `new Date()` calls. */ - setSystemTime: (time: number | string | Date) => VitestUtils + setSystemTime: (time: number | string | Date | { epochMilliseconds: number }) => VitestUtils /** * Returns mocked current date. If date is not mocked the method will return `null`. */ @@ -587,7 +587,7 @@ function createVitest(): VitestUtils { return timers().getTimerCount() }, - setSystemTime(time: number | string | Date) { + setSystemTime(time: number | string | Date | { epochMilliseconds: number }) { timers().setSystemTime(time) return utils }, diff --git a/test/unit/test/timers-temporal.test.ts b/test/unit/test/timers-temporal.test.ts index 697e8e012da9..59eb57b92ca5 100644 --- a/test/unit/test/timers-temporal.test.ts +++ b/test/unit/test/timers-temporal.test.ts @@ -44,3 +44,13 @@ it('Temporal.Now follows setSystemTime without fake timers', () => { expect(globalThis.Temporal).toBe(real) expect(globalThis.Temporal.Now.instant().epochMilliseconds).not.toBe(1234) }) + +it('setSystemTime accepts Temporal.Instant', () => { + vi.setSystemTime(globalThis.Temporal.Instant.from('2022-02-02T12:00:00Z')) + expect(new Date().toISOString()).toBe('2022-02-02T12:00:00.000Z') + expect(globalThis.Temporal.Now.instant().toString()).toBe('2022-02-02T12:00:00Z') + + vi.useFakeTimers() + vi.setSystemTime(globalThis.Temporal.Instant.fromEpochMilliseconds(1234)) + expect(Date.now()).toBe(1234) +}) From b83379f3c9cc086e78780df9182d3ed13fdd43d3 Mon Sep 17 00:00:00 2001 From: Prajwal2246 <213174436+Prajwal2246@users.noreply.github.com> Date: Thu, 24 Sep 2026 11:31:57 +0530 Subject: [PATCH 02/13] feat: support Temporal in fake timers --- docs/api/vi.md | 6 +++-- .../vitest/src/integrations/mock/timers.ts | 25 +++++++++++++++++-- packages/vitest/src/integrations/vi.ts | 10 ++++---- test/unit/test/timers-temporal.test.ts | 18 +++++++++++++ 4 files changed, 50 insertions(+), 9 deletions(-) diff --git a/docs/api/vi.md b/docs/api/vi.md index 80c5dcf1cf02..637514cb136a 100644 --- a/docs/api/vi.md +++ b/docs/api/vi.md @@ -1163,14 +1163,14 @@ await vi.runOnlyPendingTimersAsync() ### vi.setSystemTime ```ts -function setSystemTime(date: string | number | Date | Temporal.Instant): Vitest +function setSystemTime(date: string | number | Date | Temporal.Instant | Temporal.ZonedDateTime): Vitest ``` If fake timers are enabled, this method simulates a user changing the system clock (will affect date related API like `hrtime`, `performance.now` or `new Date()`) - however, it will not fire any timers. If fake timers are not enabled, this method will only mock `Date.*` and `Temporal.Now.*` calls. Useful if you need to test anything that depends on the current date - for example [Luxon](https://github.com/moment/luxon/) calls inside your code. -Accepts the same string and number arguments as the `Date`, or a `Temporal.Instant`. +Accepts the same string and number arguments as the `Date`, or a `Temporal.Instant` or `Temporal.ZonedDateTime`. A `ZonedDateTime` sets the instant only; it does not change the system time zone. ```ts const date = new Date(1998, 11, 19) @@ -1195,6 +1195,8 @@ Mocking `nextTick` is not supported when running Vitest inside `node:child_proce The implementation is based internally on [`@sinonjs/fake-timers`](https://github.com/sinonjs/fake-timers). +The `now` option accepts a `Date`, number, `Temporal.Instant`, or `Temporal.ZonedDateTime`. A `ZonedDateTime` sets the instant only; it does not change the system time zone. + ::: tip `vi.useFakeTimers()` does not automatically mock `process.nextTick` and `queueMicrotask`. But you can enable it by specifying the option in `toFake` argument: `vi.useFakeTimers({ toFake: ['nextTick', 'queueMicrotask'] })`. diff --git a/packages/vitest/src/integrations/mock/timers.ts b/packages/vitest/src/integrations/mock/timers.ts index ac044371b90a..05cc826d60e9 100644 --- a/packages/vitest/src/integrations/mock/timers.ts +++ b/packages/vitest/src/integrations/mock/timers.ts @@ -16,6 +16,26 @@ import { isChildProcess } from '../../runtime/utils' const RealDate = globalThis.Date +type TemporalNamespace = typeof globalThis extends { Temporal: infer TTemporal } + ? TTemporal + : Record + +type TemporalInstance + = TemporalNamespace extends Record infer TInstance> + ? TInstance + : never + +export type TemporalTime = TemporalInstance<'Instant'> | TemporalInstance<'ZonedDateTime'> +export type TemporalFakeTimersConfig = Omit & { + now?: number | Date | TemporalTime +} + +function toEpochMilliseconds(time: string | FakeTimersConfig['now']) { + return time && typeof time === 'object' && !(time instanceof Date) + ? time.epochMilliseconds + : time +} + export class FakeTimers { private _global: typeof globalThis private _clock!: Clock @@ -200,8 +220,9 @@ export class FakeTimers { } } - setSystemTime(now?: string | number | Date | { epochMilliseconds: number }): void { - const date = (typeof now === 'undefined' || now instanceof Date) ? now : new Date(typeof now === 'object' ? now.epochMilliseconds : now) + setSystemTime(now?: string | number | Date | TemporalTime): void { + const normalized = toEpochMilliseconds(now) + const date = (typeof normalized === 'undefined' || normalized instanceof Date) ? normalized : new Date(normalized) if (this._fakingTime) { this._clock.setSystemTime(date) } diff --git a/packages/vitest/src/integrations/vi.ts b/packages/vitest/src/integrations/vi.ts index 4691864dc500..0d637a220990 100644 --- a/packages/vitest/src/integrations/vi.ts +++ b/packages/vitest/src/integrations/vi.ts @@ -1,4 +1,3 @@ -import type { Config as FakeTimersConfig } from '@sinonjs/fake-timers' import type { MaybeMocked, MaybeMockedDeep, @@ -10,6 +9,7 @@ import type { Disposable } from 'vitest/optional-runtime-types.js' import type { RuntimeOptions, SerializedConfig } from '../runtime/config' import type { VitestMocker } from '../runtime/moduleRunner/moduleMocker' import type { MockFactoryWithHelper, MockOptions } from '../types/mocker' +import type { TemporalFakeTimersConfig, TemporalTime } from './mock/timers' import { clearAllMocks, fn, isMockFunction, resetAllMocks, restoreAllMocks, spyOn } from '@vitest/spy' import { assertTypes, createSimpleStackTrace } from '@vitest/utils/helpers' import { getSafeTimers } from '@vitest/utils/timers' @@ -29,7 +29,7 @@ export interface VitestUtils { /** * This method wraps all further calls to timers until [`vi.useRealTimers()`](https://vitest.dev/api/vi#vi-userealtimers) is called. */ - useFakeTimers: (config?: FakeTimersConfig) => VitestUtils + useFakeTimers: (config?: TemporalFakeTimersConfig) => VitestUtils /** * Restores mocked timers to their original implementations. All timers that were scheduled before will be discarded. */ @@ -86,7 +86,7 @@ export interface VitestUtils { * If fake timers are enabled, this method simulates a user changing the system clock (will affect date related API like `hrtime`, `performance.now` or `new Date()`) - however, it will not fire any timers. * If fake timers are not enabled, this method will only mock `Date.*` and `new Date()` calls. */ - setSystemTime: (time: number | string | Date | { epochMilliseconds: number }) => VitestUtils + setSystemTime: (time: number | string | Date | TemporalTime) => VitestUtils /** * Returns mocked current date. If date is not mocked the method will return `null`. */ @@ -501,7 +501,7 @@ function createVitest(): VitestUtils { const _envBooleans = ['PROD', 'DEV', 'SSR'] const utils: VitestUtils = { - useFakeTimers(config?: FakeTimersConfig) { + useFakeTimers(config?: TemporalFakeTimersConfig) { if (isChildProcess()) { if ( config?.toFake?.includes('nextTick') @@ -587,7 +587,7 @@ function createVitest(): VitestUtils { return timers().getTimerCount() }, - setSystemTime(time: number | string | Date | { epochMilliseconds: number }) { + setSystemTime(time: number | string | Date | TemporalTime) { timers().setSystemTime(time) return utils }, diff --git a/test/unit/test/timers-temporal.test.ts b/test/unit/test/timers-temporal.test.ts index 59eb57b92ca5..b670d7c19742 100644 --- a/test/unit/test/timers-temporal.test.ts +++ b/test/unit/test/timers-temporal.test.ts @@ -54,3 +54,21 @@ it('setSystemTime accepts Temporal.Instant', () => { vi.setSystemTime(globalThis.Temporal.Instant.fromEpochMilliseconds(1234)) expect(Date.now()).toBe(1234) }) + +it('setSystemTime accepts Temporal.ZonedDateTime', () => { + vi.setSystemTime(globalThis.Temporal.ZonedDateTime.from('2022-02-02T17:30:00+05:30[Asia/Kolkata]')) + expect(new Date().toISOString()).toBe('2022-02-02T12:00:00.000Z') + + vi.useFakeTimers() + vi.setSystemTime(globalThis.Temporal.ZonedDateTime.from('1970-01-01T05:30:01.234+05:30[Asia/Kolkata]')) + expect(Date.now()).toBe(1234) +}) + +it('useFakeTimers accepts Temporal as now', () => { + vi.useFakeTimers({ now: globalThis.Temporal.Instant.fromEpochMilliseconds(1234) }) + expect(Date.now()).toBe(1234) + + vi.useRealTimers() + vi.useFakeTimers({ now: globalThis.Temporal.ZonedDateTime.from('2022-02-02T17:30:00+05:30[Asia/Kolkata]') }) + expect(new Date().toISOString()).toBe('2022-02-02T12:00:00.000Z') +}) From 9c4e5441617b415808e4bf32de3f782eaa6e35b2 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa <4232207+hi-ogawa@users.noreply.github.com> Date: Mon, 28 Sep 2026 16:10:17 +0900 Subject: [PATCH 03/13] refactor(vitest): reuse sinon temporal types Co-authored-by: Codex (GPT-6) --- packages/vitest/src/integrations/mock/timers.ts | 17 ++--------------- packages/vitest/src/integrations/vi.ts | 10 +++++----- 2 files changed, 7 insertions(+), 20 deletions(-) diff --git a/packages/vitest/src/integrations/mock/timers.ts b/packages/vitest/src/integrations/mock/timers.ts index 05cc826d60e9..9c579efab578 100644 --- a/packages/vitest/src/integrations/mock/timers.ts +++ b/packages/vitest/src/integrations/mock/timers.ts @@ -10,26 +10,13 @@ import type { FakeMethod, Config as FakeTimersConfig, FakeTimers as FakeTimersContext, + TemporalTimelike, } from '@sinonjs/fake-timers' import { withGlobal } from '@sinonjs/fake-timers' import { isChildProcess } from '../../runtime/utils' const RealDate = globalThis.Date -type TemporalNamespace = typeof globalThis extends { Temporal: infer TTemporal } - ? TTemporal - : Record - -type TemporalInstance - = TemporalNamespace extends Record infer TInstance> - ? TInstance - : never - -export type TemporalTime = TemporalInstance<'Instant'> | TemporalInstance<'ZonedDateTime'> -export type TemporalFakeTimersConfig = Omit & { - now?: number | Date | TemporalTime -} - function toEpochMilliseconds(time: string | FakeTimersConfig['now']) { return time && typeof time === 'object' && !(time instanceof Date) ? time.epochMilliseconds @@ -220,7 +207,7 @@ export class FakeTimers { } } - setSystemTime(now?: string | number | Date | TemporalTime): void { + setSystemTime(now?: string | number | Date | TemporalTimelike): void { const normalized = toEpochMilliseconds(now) const date = (typeof normalized === 'undefined' || normalized instanceof Date) ? normalized : new Date(normalized) if (this._fakingTime) { diff --git a/packages/vitest/src/integrations/vi.ts b/packages/vitest/src/integrations/vi.ts index 0d637a220990..f0e66318de04 100644 --- a/packages/vitest/src/integrations/vi.ts +++ b/packages/vitest/src/integrations/vi.ts @@ -1,3 +1,4 @@ +import type { Config as FakeTimersConfig, TemporalTimelike } from '@sinonjs/fake-timers' import type { MaybeMocked, MaybeMockedDeep, @@ -9,7 +10,6 @@ import type { Disposable } from 'vitest/optional-runtime-types.js' import type { RuntimeOptions, SerializedConfig } from '../runtime/config' import type { VitestMocker } from '../runtime/moduleRunner/moduleMocker' import type { MockFactoryWithHelper, MockOptions } from '../types/mocker' -import type { TemporalFakeTimersConfig, TemporalTime } from './mock/timers' import { clearAllMocks, fn, isMockFunction, resetAllMocks, restoreAllMocks, spyOn } from '@vitest/spy' import { assertTypes, createSimpleStackTrace } from '@vitest/utils/helpers' import { getSafeTimers } from '@vitest/utils/timers' @@ -29,7 +29,7 @@ export interface VitestUtils { /** * This method wraps all further calls to timers until [`vi.useRealTimers()`](https://vitest.dev/api/vi#vi-userealtimers) is called. */ - useFakeTimers: (config?: TemporalFakeTimersConfig) => VitestUtils + useFakeTimers: (config?: FakeTimersConfig) => VitestUtils /** * Restores mocked timers to their original implementations. All timers that were scheduled before will be discarded. */ @@ -86,7 +86,7 @@ export interface VitestUtils { * If fake timers are enabled, this method simulates a user changing the system clock (will affect date related API like `hrtime`, `performance.now` or `new Date()`) - however, it will not fire any timers. * If fake timers are not enabled, this method will only mock `Date.*` and `new Date()` calls. */ - setSystemTime: (time: number | string | Date | TemporalTime) => VitestUtils + setSystemTime: (time: number | string | Date | TemporalTimelike) => VitestUtils /** * Returns mocked current date. If date is not mocked the method will return `null`. */ @@ -501,7 +501,7 @@ function createVitest(): VitestUtils { const _envBooleans = ['PROD', 'DEV', 'SSR'] const utils: VitestUtils = { - useFakeTimers(config?: TemporalFakeTimersConfig) { + useFakeTimers(config?: FakeTimersConfig) { if (isChildProcess()) { if ( config?.toFake?.includes('nextTick') @@ -587,7 +587,7 @@ function createVitest(): VitestUtils { return timers().getTimerCount() }, - setSystemTime(time: number | string | Date | TemporalTime) { + setSystemTime(time: number | string | Date | TemporalTimelike) { timers().setSystemTime(time) return utils }, From bd95246da3ea5a106124ffa00695a227bcbbc0cf Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa <4232207+hi-ogawa@users.noreply.github.com> Date: Mon, 28 Sep 2026 16:11:06 +0900 Subject: [PATCH 04/13] nit --- packages/vitest/src/integrations/mock/timers.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/vitest/src/integrations/mock/timers.ts b/packages/vitest/src/integrations/mock/timers.ts index 9c579efab578..4c1404ae8874 100644 --- a/packages/vitest/src/integrations/mock/timers.ts +++ b/packages/vitest/src/integrations/mock/timers.ts @@ -17,12 +17,6 @@ import { isChildProcess } from '../../runtime/utils' const RealDate = globalThis.Date -function toEpochMilliseconds(time: string | FakeTimersConfig['now']) { - return time && typeof time === 'object' && !(time instanceof Date) - ? time.epochMilliseconds - : time -} - export class FakeTimers { private _global: typeof globalThis private _clock!: Clock @@ -282,3 +276,9 @@ export class FakeTimers { return this._fakingTime } } + +function toEpochMilliseconds(time: string | FakeTimersConfig['now']) { + return time && typeof time === 'object' && !(time instanceof Date) + ? time.epochMilliseconds + : time +} From 974810a79c6b2d508246855ad196519d3ffa2ce0 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa <4232207+hi-ogawa@users.noreply.github.com> Date: Mon, 28 Sep 2026 16:12:35 +0900 Subject: [PATCH 05/13] test(vitest): consolidate temporal timer cases Co-authored-by: Codex (GPT-6) --- test/unit/test/timers-temporal.test.ts | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/test/unit/test/timers-temporal.test.ts b/test/unit/test/timers-temporal.test.ts index b670d7c19742..a087156d86fe 100644 --- a/test/unit/test/timers-temporal.test.ts +++ b/test/unit/test/timers-temporal.test.ts @@ -45,20 +45,11 @@ it('Temporal.Now follows setSystemTime without fake timers', () => { expect(globalThis.Temporal.Now.instant().epochMilliseconds).not.toBe(1234) }) -it('setSystemTime accepts Temporal.Instant', () => { +it('setSystemTime accepts Temporal.Instant and Temporal.ZonedDateTime', () => { vi.setSystemTime(globalThis.Temporal.Instant.from('2022-02-02T12:00:00Z')) expect(new Date().toISOString()).toBe('2022-02-02T12:00:00.000Z') expect(globalThis.Temporal.Now.instant().toString()).toBe('2022-02-02T12:00:00Z') - vi.useFakeTimers() - vi.setSystemTime(globalThis.Temporal.Instant.fromEpochMilliseconds(1234)) - expect(Date.now()).toBe(1234) -}) - -it('setSystemTime accepts Temporal.ZonedDateTime', () => { - vi.setSystemTime(globalThis.Temporal.ZonedDateTime.from('2022-02-02T17:30:00+05:30[Asia/Kolkata]')) - expect(new Date().toISOString()).toBe('2022-02-02T12:00:00.000Z') - vi.useFakeTimers() vi.setSystemTime(globalThis.Temporal.ZonedDateTime.from('1970-01-01T05:30:01.234+05:30[Asia/Kolkata]')) expect(Date.now()).toBe(1234) From 86206859510b49a79fa1fd2fb1b60662bf307754 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa <4232207+hi-ogawa@users.noreply.github.com> Date: Mon, 28 Sep 2026 16:14:37 +0900 Subject: [PATCH 06/13] test(vitest): align temporal timer assertions Co-authored-by: Codex (GPT-6) --- test/unit/test/timers-temporal.test.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/test/unit/test/timers-temporal.test.ts b/test/unit/test/timers-temporal.test.ts index a087156d86fe..f5111e411067 100644 --- a/test/unit/test/timers-temporal.test.ts +++ b/test/unit/test/timers-temporal.test.ts @@ -46,18 +46,19 @@ it('Temporal.Now follows setSystemTime without fake timers', () => { }) it('setSystemTime accepts Temporal.Instant and Temporal.ZonedDateTime', () => { - vi.setSystemTime(globalThis.Temporal.Instant.from('2022-02-02T12:00:00Z')) - expect(new Date().toISOString()).toBe('2022-02-02T12:00:00.000Z') - expect(globalThis.Temporal.Now.instant().toString()).toBe('2022-02-02T12:00:00Z') + vi.setSystemTime(globalThis.Temporal.Instant.fromEpochMilliseconds(1234)) + expect(Date.now()).toBe(1234) + expect(globalThis.Temporal.Now.instant().epochMilliseconds).toBe(1234) vi.useFakeTimers() - vi.setSystemTime(globalThis.Temporal.ZonedDateTime.from('1970-01-01T05:30:01.234+05:30[Asia/Kolkata]')) - expect(Date.now()).toBe(1234) + vi.setSystemTime(globalThis.Temporal.ZonedDateTime.from('2022-02-02T17:30:00+05:30[Asia/Kolkata]')) + expect(new Date().toISOString()).toBe('2022-02-02T12:00:00.000Z') }) it('useFakeTimers accepts Temporal as now', () => { vi.useFakeTimers({ now: globalThis.Temporal.Instant.fromEpochMilliseconds(1234) }) expect(Date.now()).toBe(1234) + expect(globalThis.Temporal.Now.instant().epochMilliseconds).toBe(1234) vi.useRealTimers() vi.useFakeTimers({ now: globalThis.Temporal.ZonedDateTime.from('2022-02-02T17:30:00+05:30[Asia/Kolkata]') }) From edd2a645945c94f25234fa5892ad1f0296905400 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa <4232207+hi-ogawa@users.noreply.github.com> Date: Mon, 28 Sep 2026 16:17:06 +0900 Subject: [PATCH 07/13] fix(vitest): guard temporal time conversion Co-authored-by: Codex (GPT-6) --- packages/vitest/src/integrations/mock/timers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vitest/src/integrations/mock/timers.ts b/packages/vitest/src/integrations/mock/timers.ts index 4c1404ae8874..3219bda19fa0 100644 --- a/packages/vitest/src/integrations/mock/timers.ts +++ b/packages/vitest/src/integrations/mock/timers.ts @@ -278,7 +278,7 @@ export class FakeTimers { } function toEpochMilliseconds(time: string | FakeTimersConfig['now']) { - return time && typeof time === 'object' && !(time instanceof Date) + return time && typeof time === 'object' && 'epochMilliseconds' in time ? time.epochMilliseconds : time } From 97017842cbd87f747183547bab6f43c3355b13ae Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa <4232207+hi-ogawa@users.noreply.github.com> Date: Mon, 28 Sep 2026 16:18:25 +0900 Subject: [PATCH 08/13] refactor(vitest): align time conversion types Co-authored-by: Codex (GPT-6) --- packages/vitest/src/integrations/mock/timers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vitest/src/integrations/mock/timers.ts b/packages/vitest/src/integrations/mock/timers.ts index 3219bda19fa0..6dab1a57dfe5 100644 --- a/packages/vitest/src/integrations/mock/timers.ts +++ b/packages/vitest/src/integrations/mock/timers.ts @@ -277,7 +277,7 @@ export class FakeTimers { } } -function toEpochMilliseconds(time: string | FakeTimersConfig['now']) { +function toEpochMilliseconds(time?: string | number | Date | TemporalTimelike) { return time && typeof time === 'object' && 'epochMilliseconds' in time ? time.epochMilliseconds : time From 4f5974716262d0b0f7588e359ae623bd400db622 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa <4232207+hi-ogawa@users.noreply.github.com> Date: Mon, 28 Sep 2026 16:20:00 +0900 Subject: [PATCH 09/13] docs(vitest): explain Temporal conversion Co-authored-by: Codex (GPT-6) --- packages/vitest/src/integrations/mock/timers.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/vitest/src/integrations/mock/timers.ts b/packages/vitest/src/integrations/mock/timers.ts index 6dab1a57dfe5..e521917204e3 100644 --- a/packages/vitest/src/integrations/mock/timers.ts +++ b/packages/vitest/src/integrations/mock/timers.ts @@ -277,6 +277,7 @@ export class FakeTimers { } } +// Like fake-timers, read epochMilliseconds from Instant or ZonedDateTime without requiring built-in Temporal types. function toEpochMilliseconds(time?: string | number | Date | TemporalTimelike) { return time && typeof time === 'object' && 'epochMilliseconds' in time ? time.epochMilliseconds From 05e6ff07c0f1cc3514e5582b5a0982eca10ce655 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa <4232207+hi-ogawa@users.noreply.github.com> Date: Mon, 28 Sep 2026 16:21:35 +0900 Subject: [PATCH 10/13] refactor(vitest): clarify time normalization Co-authored-by: Codex (GPT-6) --- packages/vitest/src/integrations/mock/timers.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/vitest/src/integrations/mock/timers.ts b/packages/vitest/src/integrations/mock/timers.ts index e521917204e3..27a8b51bbdf0 100644 --- a/packages/vitest/src/integrations/mock/timers.ts +++ b/packages/vitest/src/integrations/mock/timers.ts @@ -202,7 +202,7 @@ export class FakeTimers { } setSystemTime(now?: string | number | Date | TemporalTimelike): void { - const normalized = toEpochMilliseconds(now) + const normalized = normalizeSystemTime(now) const date = (typeof normalized === 'undefined' || normalized instanceof Date) ? normalized : new Date(normalized) if (this._fakingTime) { this._clock.setSystemTime(date) @@ -278,7 +278,7 @@ export class FakeTimers { } // Like fake-timers, read epochMilliseconds from Instant or ZonedDateTime without requiring built-in Temporal types. -function toEpochMilliseconds(time?: string | number | Date | TemporalTimelike) { +function normalizeSystemTime(time?: string | number | Date | TemporalTimelike): string | number | Date | undefined { return time && typeof time === 'object' && 'epochMilliseconds' in time ? time.epochMilliseconds : time From aab8659cb536e4391511555d74ae98201a942e95 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa <4232207+hi-ogawa@users.noreply.github.com> Date: Mon, 28 Sep 2026 16:27:47 +0900 Subject: [PATCH 11/13] docs(vitest): clarify ZonedDateTime behavior Co-authored-by: Codex (GPT-6) --- docs/api/vi.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/api/vi.md b/docs/api/vi.md index 637514cb136a..05b2036c4f5b 100644 --- a/docs/api/vi.md +++ b/docs/api/vi.md @@ -1170,7 +1170,7 @@ If fake timers are enabled, this method simulates a user changing the system clo Useful if you need to test anything that depends on the current date - for example [Luxon](https://github.com/moment/luxon/) calls inside your code. -Accepts the same string and number arguments as the `Date`, or a `Temporal.Instant` or `Temporal.ZonedDateTime`. A `ZonedDateTime` sets the instant only; it does not change the system time zone. +Accepts the same string and number arguments as the `Date`, or a `Temporal.Instant` or `Temporal.ZonedDateTime`. A `ZonedDateTime` sets the instant but does not change the system time zone. ```ts const date = new Date(1998, 11, 19) @@ -1195,7 +1195,7 @@ Mocking `nextTick` is not supported when running Vitest inside `node:child_proce The implementation is based internally on [`@sinonjs/fake-timers`](https://github.com/sinonjs/fake-timers). -The `now` option accepts a `Date`, number, `Temporal.Instant`, or `Temporal.ZonedDateTime`. A `ZonedDateTime` sets the instant only; it does not change the system time zone. +The `now` option accepts a `Date`, number, `Temporal.Instant`, or `Temporal.ZonedDateTime`. A `ZonedDateTime` sets the instant but does not change the system time zone. ::: tip `vi.useFakeTimers()` does not automatically mock `process.nextTick` and `queueMicrotask`. From fec4fab8a6eec5a08f5c397f5c50863bc5e82e9c Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa <4232207+hi-ogawa@users.noreply.github.com> Date: Mon, 28 Sep 2026 16:29:41 +0900 Subject: [PATCH 12/13] docs(vitest): clarify zoned mocked time Co-authored-by: Codex (GPT-6) --- docs/api/vi.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/api/vi.md b/docs/api/vi.md index 05b2036c4f5b..363cf4d44fba 100644 --- a/docs/api/vi.md +++ b/docs/api/vi.md @@ -1170,7 +1170,7 @@ If fake timers are enabled, this method simulates a user changing the system clo Useful if you need to test anything that depends on the current date - for example [Luxon](https://github.com/moment/luxon/) calls inside your code. -Accepts the same string and number arguments as the `Date`, or a `Temporal.Instant` or `Temporal.ZonedDateTime`. A `ZonedDateTime` sets the instant but does not change the system time zone. +Accepts the same string and number arguments as the `Date`, or a `Temporal.Instant` or `Temporal.ZonedDateTime`. Vitest uses the moment represented by a `ZonedDateTime` as the mocked time and leaves the environment's time zone unchanged. ```ts const date = new Date(1998, 11, 19) @@ -1195,7 +1195,7 @@ Mocking `nextTick` is not supported when running Vitest inside `node:child_proce The implementation is based internally on [`@sinonjs/fake-timers`](https://github.com/sinonjs/fake-timers). -The `now` option accepts a `Date`, number, `Temporal.Instant`, or `Temporal.ZonedDateTime`. A `ZonedDateTime` sets the instant but does not change the system time zone. +The `now` option accepts a `Date`, number, `Temporal.Instant`, or `Temporal.ZonedDateTime`. Vitest uses the moment represented by a `ZonedDateTime` as the mocked time and leaves the environment's time zone unchanged. ::: tip `vi.useFakeTimers()` does not automatically mock `process.nextTick` and `queueMicrotask`. From bad6122e4411a9cd61ff2bb4cbcb302ef63dda49 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa <4232207+hi-ogawa@users.noreply.github.com> Date: Mon, 28 Sep 2026 16:30:08 +0900 Subject: [PATCH 13/13] docs(vitest): lead with fake timer options Co-authored-by: Codex (GPT-6) --- docs/api/vi.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/api/vi.md b/docs/api/vi.md index 363cf4d44fba..aa678f3c9b76 100644 --- a/docs/api/vi.md +++ b/docs/api/vi.md @@ -1193,10 +1193,10 @@ To enable mocking timers, you need to call this method. It will wrap all further Mocking `nextTick` is not supported when running Vitest inside `node:child_process` by using `--pool=forks`. NodeJS uses `process.nextTick` internally in `node:child_process` and hangs when it is mocked. Mocking `nextTick` is supported when running Vitest with `--pool=threads`. -The implementation is based internally on [`@sinonjs/fake-timers`](https://github.com/sinonjs/fake-timers). - The `now` option accepts a `Date`, number, `Temporal.Instant`, or `Temporal.ZonedDateTime`. Vitest uses the moment represented by a `ZonedDateTime` as the mocked time and leaves the environment's time zone unchanged. +The implementation is based internally on [`@sinonjs/fake-timers`](https://github.com/sinonjs/fake-timers). + ::: tip `vi.useFakeTimers()` does not automatically mock `process.nextTick` and `queueMicrotask`. But you can enable it by specifying the option in `toFake` argument: `vi.useFakeTimers({ toFake: ['nextTick', 'queueMicrotask'] })`.