Vitest timezone mocking: inconsistent Date behavior across environments
0 reputation · 03 Feb 2025, 13:01 UTC
Problem Overview
Vitest uses the Node.js Date implementation, which follows the process.env.TZ setting. When tests involve date‑manipulation libraries such as date‑fns‑tz, the output can vary between a developer’s machine and CI runners that have different default time zones.
Current Constraints
- Vitest provides no built‑in helper to override or mock the system timezone within a test suite.
- Modifying
process.env.TZaffects all subsequentDatecalls and can interfere with other tests or parallel workers. - Third‑party mocking utilities may introduce version‑specific quirks that are not guaranteed across Vitest releases.
Unresolved Decision
Teams must decide whether to rely on manual TZ manipulation, adopt external libraries, or push for a first‑party Vitest API that standardizes timezone mocking. The lack of a documented approach leaves uncertainty about how to achieve consistent, repeatable test results across environments.
Key Questions
- Should Vitest expose a dedicated API for setting or mocking the process timezone within a test suite?
- If not, what is the safest pattern for ensuring consistent timezone behavior across parallel workers and CI pipelines?
- How can we guarantee that third‑party timezone libraries honor the mocked timezone without affecting unrelated tests?