Skip to content

.tip

· one of the small things

Tests that touch time use a fake clock

A test that calls Date.now() passes at 2pm and fails at midnight, or on the last day of February. Inject the clock, or freeze it.

vi.useFakeTimers();
vi.setSystemTime(new Date("2026-02-28T23:59:00Z"));
expect(isExpired(token)).toBe(false);
vi.advanceTimersByTime(120_000);
expect(isExpired(token)).toBe(true);

The same goes for setTimeout and intervals. Moving a fake clock forward beats sleeping in a test.

testing