Mocking Dates & Temporal in Vitest
From the Vitest cheat sheet · Fake Timers & Dates · verified Nov 2026
Mocking Dates & Temporal
Freeze or set the current time for date-dependent code
typescript
vi.useFakeTimers()
vi.setSystemTime(new Date('2026-01-01T09:00:00Z'))
expect(new Date().toISOString()).toBe('2026-01-01T09:00:00.000Z')
vi.useRealTimers()💡 setSystemTime works with or without fake timers
📌 v5 fakes Temporal.Now alongside Date when Temporal is available
⚠️ Local-time dates depend on TZ; set TZ=UTC in CI for stable tests
🟢 Always restore with vi.useRealTimers() in afterEach
datessetSystemTimetemporal
Continue with Vitest
Save the full cheat sheet or work through every related task.