TIME IN TESTS

A test that reads the clock passes on the day it was written

L215 MINTESTING
PREDICT

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.

▶ SIMULATION
CALENDAR
now: not started
user zone: —
THE SUITE
setup: the code and the test both read the system clock
builds run: 0 · red: 0
green with a wrong report: 0
THE TEAM SEES
last build: —
edge cases driven each run: 0
last—
✓ the build is green exactly when the report is right for the user✓ the same commit gets the same verdict on every day it runs
SCHEDULE
01[CI]test setup: the code and the test both read the system clock
02[CALENDAR]the nightly job runs: an ordinary Tuesday
03[CALENDAR]the nightly job runs: the next day
04[CALENDAR]the nightly job runs: a mid-July afternoon
05[CI]report
Start of ordinary days scenario. 5 steps.
// 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

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.

BREAK IT NOT YET

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

FIX IT NOT YET

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.

Mental model:

Time is an input, and a function that reads it for itself has an input the test cannot set. Pass the clock in, pass the zone with it, and write the test for the dates where calendars misbehave on purpose, instead of hoping CI happens to run on one.

IN PRODUCTION

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.

IN INTERVIEWS

“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.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: RETRIES HIDE THE BUG →