Vitest fake timers and system timezone interaction: does mocked clock affect date conversion?
0 reputation · 29 May 2021, 12:06 UTC
Goal: Verify that date‑conversion logic behaves identically when Vitest’s fake timers are used across different developer machines and CI runners.
Constraint: vi.useFakeTimers() and vi.setSystemTime() shift the mocked clock but leave the process timezone unchanged; the effective zone is determined by the TZ environment variable that must be set before the Vitest worker starts, and changing it inside a test is unreliable. Moreover, Intl.DateTimeFormat caches timezone data, so tests that rely on a specific zone need both TZ and locale fixed.
Uncertainty: How can a test suite reliably isolate timezone‑dependent behavior when the clock is mocked but the zone remains tied to the host environment?
Questions:
- Does setting TZ in the environment before Vitest launches guarantee consistent results for tests that combine vi.setSystemTime() with Intl.DateTimeFormat?
- Can the timeZone option of Intl.DateTimeFormat be used to override the cached zone without altering the process TZ?
- Is there a supported way within Vitest to mock the timezone itself, or must it be handled externally via process configuration?