Two test assumptions that broke on a real database: a fake clock and an empty table
A real-database test suite went from 310 passing to 308 without an app bug. One test faked the clock to 2031, which made a real login token look expired once the API verified tokens locally; another asserted totals over a table real people had started using. Both reproduced in Node.js, with the fixes: inject the clock where it's needed, and assert only on rows the test created.
A test suite that runs against a real database had 308 of 310 tests passing, and neither failure was an application bug. Both were assumptions the tests made about the world that stopped being true. One test faked the system clock to 2031 to check a date rule. When the API started verifying login tokens locally, the server compared the token’s expiry with that fake clock, and a real one-hour token became “expired”, so the request got a 401. The other test asserted totals over a whole table, assuming nobody else wrote to it. Once real people filed reports in the same project, their rows joined the totals. The fixes: pass a clock to the code that needs it instead of faking time for the whole process, and assert only on rows the test created. After both changes, 310 of 310 passed again.

Neither failure was a bug in the app. Both were facts the tests took for granted.
Why fake the clock at all?
The CRM I built stores sales reports by day, and “today” means today in Asia/Karachi, not in UTC. At 20:00 UTC it’s already 01:00 the next day in Karachi, so a report filed then belongs to tomorrow. The original test checked that rule through the API by freezing Date:
vi.useFakeTimers({ toFake: ["Date"] }); vi.setSystemTime(new Date("2031-06-10T20:00:00Z")); const r = await api(sdr, "GET", "/api/reports/today"); // the request carries a real login token expect(getToday()).toBe("2031-06-11");
That worked while the server checked tokens by calling the auth service, which used its own clock. After the switch to local verification, the server read the expiry from the token and compared it with Date.now(), which the test had moved five years ahead.
Can you reproduce the fake-clock failure?
With Node’s own test runner and its mock timers, against a local ES256 verifier:
test("a test that sets Date to 2031 gets 'expired' for a real token", async () => { const token = await sign({ sub: "u1", iss: issuer, exp: now + 3600 }); mock.timers.enable({ apis: ["Date"], now: new Date("2031-06-10T20:00:00Z") }); try { await assert.rejects(verify(token), /expired/); } finally { mock.timers.reset(); } assert.ok(await verify(token)); // with the real clock, the same token is fine });
It fails exactly as the suite did: a token valid for an hour is “expired” under the fake date, and valid again once the real clock returns. Any other JavaScript in the process that reads Date (cookie expiry, rate-limit windows, cache TTLs) sees the same fake date.
What’s the fix for the clock?
Give the code a clock instead of changing the world’s. The date rule takes the current time as an argument, with the real clock as the default:
export const today = (clock = Date.now) => new Intl.DateTimeFormat("en-CA", { timeZone: "Asia/Karachi" }).format(clock()); today(() => Date.parse("2031-06-10T20:00:00Z")); // "2031-06-11": 20:00 UTC is already tomorrow in Karachi
The rule keeps its exact test, as a unit test with no login and no network. The API test then checks only what it can check honestly with real time: the route answers 200 and returns today’s date in YYYY-MM-DD form. In the CRM that’s how the test was rewritten, with a comment explaining why it no longer fakes the clock.
What went wrong with the totals?
The history report sums replies per week. The test saved three report rows for its own user and asserted the week’s totals:
save("test-sdr", "2026-11-23", 1); save("test-sdr", "2026-11-23", 10); save("test-sdr", "2026-11-25", 2); week(); // { rows: 3, people: 1, replies: 13 }: true while the project was empty save("real-sdr", "2026-11-24", 7); // a real person files a report in the same week week(); // { rows: 4, people: 2, replies: 20 } week("test-sdr"); // { rows: 3, people: 1, replies: 13 }: scoped to the test's own rows
On day one the assertion passed because the database held nothing else. The suite runs against the live project’s database (with its own test users), so as soon as the sales team used the app, a whole-table total included their work. The fix was to filter by the test’s user, through the same admin-only user_id filter the app already had, and to check the “two people” fact separately by listing people rather than summing.
Rules for tests that touch shared state
- Never fake global time in a test that crosses a trust boundary. Token expiry, signed URLs and rate-limit windows all read it. Inject a clock into the one function under test.
- Assert on what you created. Tag test data (a unique prefix, a test user, a run ID) and filter every assertion by it. “The table has N rows” is only true in an empty database.
- Prefer exact checks on your own rows to loose checks on everyone’s. Loosening to “at least 13” would pass, and would also pass when the sum is wrong.
- When a test fails after an unrelated change, ask what it assumed. Here, the change that broke a date test was an auth change.
Summary
- A test that fakes
Datefor the process also fakes it for token expiry: a real token looked expired in 2031. Inject a clock into the code under test instead. - A test that asserts whole-table totals breaks as soon as anyone else writes: 3 rows became 4, 13 replies became 20. Scope assertions to the test’s own data.
- The suite went from 308/310 to 310/310 without touching application code.
The auth change behind the first failure is verifying JWTs locally with a JWKS. The same CRM’s front end had a silent failure of its own: records vanishing from a Kanban board. For more on time zones in scheduled work, see scheduling a job at 9am local time across DST.
Part of the guide Reliable backends in Node.js, Python and Go, which puts related articles in reading order.