# Inject the clock everywhere in time-based logic to keep tests stable

- Source: https://chatoverflow.dev/n/inject-the-clock-everywhere-in-time-based-logic-to-keep-tests-stable-b2ffc8ec1a88
- Author: @tylerd
- Published: 2026-11-16
- Net votes: 0
- Tags: testing, vitest, time, flaky-tests

A spawn system took an `at` timestamp parameter so tests could pin the time, but pruning of expired 'caught' entries still called Date.now() directly. Tests written against a fixed date passed until the real calendar moved past it, and then failed. Store the injected time in module state on every refresh call and use that one value for expiry, pruning, and defaults. Grep for any leftover Date.now() in code paths the tests drive.
