TIME IN TESTS
A test that reads the clock passes on the day it was written
A report endpoint returns the month so far: the 1st to yesterday. Its test reads the system clock, as the code does, and compares the two. The test was written on the 14th and has been green for six weeks. At five past midnight on the 1st of a month, CI runs it. What happens?
Code that reads the clock has a hidden input: the date. A test that reads the same clock cannot control that input, so it only ever tests the day it runs on. The edges are the month end, the leap day, the turn of the year, and the user whose local date is not the server’s. A fixed clock is only half of it. The other half is the time zone, because the same instant is two different dates in two places.
The fix is to make the clock an argument. The function takes clock and a zone, and the test sets whichever instant it wants. You get the same discipline as an order-dependent test: hidden state becomes an input you control. A test that depends on the wall clock is also a flaky test of a different kind: it fails for a reason that is deterministic, just not on a day you chose.
// reads the system clock, and the day number is arithmetic on a string
function reportRange() {
const now = new Date(); // CI's clock, in CI's time zone
const y = now.getUTCFullYear(), m = now.getUTCMonth() + 1, d = now.getUTCDate();
return { from: `${y}-${pad(m)}-01`, to: `${y}-${pad(m)}-${pad(d - 1)}` }; // day 0 on the 1st
}
// the test: expected = the same expression with the same clock// the clock and the zone are arguments
function reportRange(clock, tzHours) {
const local = new Date(clock.now() + tzHours * 3600000);
// first of the local month to yesterday; on the 1st, the whole previous month
}
// the test hands in a fake clock set to 31 Jan, 1 Mar 2028, 1 Jan, 22:30 UTC with tz +2... Watch the strip: the build is green when it should be red for the user, and red on a day nothing changed. Then step the injected-clock scenario.
The suite has been green all week. Pick one date for the fake calendar to jump to before the build runs. Make the build go red, or green with a wrong report.
GOAL: a red build, or a green build with a wrong report
Write reportRange(clock, tzHours). clock.now() returns epoch milliseconds; tzHours is the user's offset from UTC (it can be fractional, such as 5.5). Use the user's local date: return { from, to } as YYYY-MM-DD strings, from the first of the local month to yesterday. On the 1st of a month, return the whole previous month instead. Do not read the system clock yourself.
Billing cycles and renewal dates, monthly reports, trial expiry, scheduled jobs, certificate and token expiry, daily limits and anything that says “today”. Libraries make the clock injectable: java.time.Clock, Python's freezegun or time-machine, Jest fake timers with setSystemTime, Go's clock interfaces. A zone is part of the input: store instants in UTC, convert to the user's zone only at the edge. Real zones are not fixed offsets: they change with daylight saving time, so use IANA zone names such as Europe/London or America/Sao_Paulo rather than a number, and cover the transition days, when a local day is 23 or 25 hours long. The exercise here uses fixed offsets to keep the harness small.
“This test fails once a month. What do you do?” Check the dates of the failures against month ends before touching the test. Say the code reads the clock directly, make the clock an argument, and name the edges you would drive: month boundaries, 29 February and 2100, the new year, and a user in a zone where the date differs. Reject changing the CI server's time zone as a fix.