Dev.to WebDev πŸ›  Dev πŸ‘ 2 πŸ“– 4 min read

I moved the clock to 15 dates between 2025 and 2041, and all 732 of our tests passed every time

A test that fails only on the last day of the month is a rite of passage. It passes for 29 days, it fails on the thirtieth, and by the time you are looking at it the date has moved on. After the time-zone run I wanted th

A test that fails only on the last day of the month is a rite of passage. It passes for 29 days, it fails on the thirtieth, and by the time you are looking at it the date has moved on. After the time-zone run I wanted the same check for the other half of "when": what if the clock itself is somewhere else?

A clock you can set from outside

Node's test runner starts each file in a child process, and NODE_OPTIONS reaches all of them. So a module loaded with --import can replace Date before any test code runs. This is the whole thing:

// shift.mjs
const offset = Date.parse(process.env.FAKE_NOW) - Date.now();
const RealDate = Date;
class FakeDate extends RealDate {
  constructor(...a) { if (a.length === 0) super(RealDate.now() + offset); else super(...a); }
  static now() { return RealDate.now() + offset; }
}
globalThis.Date = FakeDate;

It shifts rather than freezes. The clock still ticks, it just starts at the date you give it, which matters for code that compares two readings. Arguments to new Date(...) pass through untouched, so a fixture like new Date("2026-10-26T12:00:00Z") stays exactly what it says.

FAKE_NOW=2038-01-19T03:14:00Z NODE_OPTIONS="--import /tmp/shift.mjs" pnpm test

I did not trust it before checking that it bites. A throwaway test that prints new Date().toISOString() under the same variables printed 2038-01-19T03:14:00.009Z from inside the child process, so the shim reaches the tests.

Fifteen dates

I picked the ones that break calendars:

Date (UTC) Why
2025-01-01 and 2026-10-01 before any date the code knows about
2026-10-26 12:00:01 one second past a fixture's free-week end
2026-11-30 a month end
2026-12-05 and 2026-12-06 the day I wrote this, and the Sunday after
2026-12-31 23:59:30 thirty seconds before New Year
2027-01-01 00:00:30 thirty seconds after
2027-01-31 add a month and February has no 31st
2027-03-28 01:30 UK clocks go forward, a time that happens once
2027-10-31 01:30 UK clocks go back, a time that happens twice
2028-02-29 leap day
2030-06-15 and 2041-05-05 well into the future
2038-01-19 03:14 the minute a signed 32-bit time runs out

Every one printed pass 732, fail 0.

That is a null result, and I almost did not write it up. Then I asked why, because a suite that cannot see the date at all is either well made or not testing anything that depends on it.

One test reads the clock, and thirty-six pin it

I grepped the test files for the two ways to ask for now. new Date() and Date.now() appear in one test file, once: widen.test.ts line 45, where const now = Date.now() is used as a base for offsets. Against that, there are 36 fixtures written as new Date("2026-..."): 30 in October 2026, 4 in November, one in September and one in March.

The code under test is the other side of this. Of our 64 non-test files that read the clock, 39 are loaded by a test. 18 of those 39 take now as a parameter, often with a default, as in src/lib/time.ts:

export const daysAgo = (n: number, now = Date.now()) => new Date(now - n * DAY_MS);
export function startOfDayIn(timeZone: string, now = new Date()): Date { ... }

A default argument is the cheapest dependency injection there is. Production calls startOfDayIn("Europe/London") and gets today. A test calls it with a date and gets a function of that date. The test never has to touch the clock, so moving the clock changes nothing. The Pacific-day reset for our YouTube quota works this way, and so does the wait after an out-of-office reply. The monthly allowance behind the limits on the pricing page does not: the function that rolls a subscription's month forward calls new Date() itself, which a shifted clock would change, if any test called it.

What a pass does not say

Three things I should not overclaim.

It does not say the code is right on those dates. It says the tests' answers do not depend on the date they run on. The leap-day logic is tested because a test passes a leap-day fixture, not because I ran the suite on 29 February.

It does not cover the 143 clock reads that no test reaches. The 39 loaded files hold 143 of the 204 new Date() or Date.now() calls in the app, and the rest sit in 25 files no test imports at all. The biggest of those is the job queue in src/lib/jobs.ts, with eight reads, which decides what is due and what has expired. A shifted clock cannot find a bug in a function that the suite never calls.

It does not move anything but Date. performance.now, setTimeout and the database's now() keep real time, and a test that mixes a shifted JavaScript date with a Postgres timestamp would see a gap. None of the tests I read connects to one.

What I would take from it

The one-line shim is worth keeping. It is a regression test for a habit rather than a test of a function: if someone adds new Date() inside a tested function and compares it to a fixed date, one of those fifteen runs will fail on the spot. For that reason I would put a run at 2038 and one on a leap day in CI, since they cost four seconds each.

The better habit is the one the repo already has. Take the date as an argument with a sensible default, and the question "what does this do on 29 February" becomes a test that passes a date. The calculators on the site work the same way: they run on whatever the caller hands them, and how it works describes the parts that do depend on the day.

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.