{"slug": "the-ai-generated-date-test-passes-today-and-fails-next-week", "title": "The AI-generated date test passes today and fails next week", "summary": "A developer's writeup warns that AI coding agents frequently produce unit tests that read the live system clock, so a test that passes on the day it is written can fail a week later in CI with no product change. The piece recommends injecting a clock into the code under test, freezing time with a fake clock, asserting offsets rather than hardcoded calendar dates, and exercising both sides of the expiry boundary at a single pinned instant.", "body_md": "You asked the agent to cover a subscription expiry. It wrote a unit test, ran it locally, reported green, and moved on. A week later CI goes red on that exact test. Nothing in the product changed. The test reads the real clock, so it only held under the date the agent happened to run on.\n\nThis is one of the gentlest failure modes an agent introduces: the test genuinely passed, the assertion looked correct, and the reviewer saw a green unit test and assumed determinism. A test that depends on the wall clock is not a deterministic test; it is a short-term prediction dressed up as coverage, and it quietly expires on its own schedule.\n\nThe agent cannot know the future date, and it rarely flags that its own test uses the current instant. It computes expiry from `now()`, compares against today's calendar, and the result agrees on the machine and the day it was written. The bug surfaces later, when time itself moves.\n\n**It passes on the machine that writes it.** The live clock matches the expected behavior at that instant, so green feels like proof the logic works rather than proof the logic works today.\n\n**The assertion reflects real behavior under today's time.** An expiry check that looks right under the current instant is easy to mistake for a durable test; the off-by-one lives in the future, not in the current run.\n\n**Reviewers skim green and stop.** A passing unit test carries the same visual weight regardless of whether it freezes time, so a clock read inside the test goes unnoticed.\n\n**Some agents bake in a calendar literal.** Rather than an offset, the expected value becomes a fixed date string for the week the test was written, narrowing the window to a handful of days instead of a condition that holds forever.\n\n**Midnight and timezone drift hide the cause.** A test that flips near midnight, or differs between machines in different zones, gets filed as flaky and quarantined instead of traced to the live clock.\n\n**The test calls `Date.now()`, `new Date()`, or `time.Now()` directly.** The unit under test receives no injected clock, so every assertion anchors to the real current instant.\n\n**The expected expiry is computed as today plus N inside the test.** The expectation itself moves when the clock moves, which is why the same code passes today and fails next week.\n\n**A literal calendar date appears in the expected value.** The agent hardcodes a specific day so the assertion passes only within a narrow window around writing time.\n\n**The just-expired boundary uses the current instant.** The \"already expired\" branch and the \"still active\" branch are chosen by the live clock, so moving time changes which case the test actually exercises.\n\n**Green on the author's machine, red on a later day.** The suite passes locally, passes CI the first few runs, then turns red on a date nobody changed in code.\n\n**Inject a clock into the unit under test.** Pass `nowFn` or a clock object instead of calling the system time inside the logic, so the test supplies a fixed instant rather than borrowing the real one.\n\n**Freeze time in the test.** Pin `Date.now()` or use a fake clock set to one known timestamp; every assertion derives from that fixed value.\n\n**Assert the offset, not the absolute date string.** Check that expiry equals the pinned instant plus seven days, rather than comparing against a calendar literal that ages out.\n\n**Test both sides of the boundary at the same pinned time.** Cover one millisecond before expiry and one millisecond after under the identical fixed instant, so the live clock cannot quietly select which branch you exercise.\n\n**Advance the fake clock deliberately.** Move the pinned instant forward a week or a month in the test and confirm every assertion still predicts the outcome; this catches a time bomb before CI does.\n\n`now`?\nA green date test today only proves the clock on the machine that ran it. Freeze time, assert the offset, and cover both sides of the boundary, so next week turns into a green suite rather than a red surprise.", "url": "https://wpnews.pro/news/the-ai-generated-date-test-passes-today-and-fails-next-week", "canonical_source": "https://dev.to/jeff_pdc/the-ai-generated-date-test-passes-today-and-fails-next-week-1fkl", "published_at": "2026-10-23 00:27:47+00:00", "updated_at": "2026-10-23 00:42:08.285300+00:00", "lang": "en", "topics": ["ai-agents", "ai-tools", "developer-tools", "artificial-intelligence"], "entities": [], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/the-ai-generated-date-test-passes-today-and-fails-next-week", "markdown": "https://wpnews.pro/news/the-ai-generated-date-test-passes-today-and-fails-next-week.md", "text": "https://wpnews.pro/news/the-ai-generated-date-test-passes-today-and-fails-next-week.txt", "jsonld": "https://wpnews.pro/news/the-ai-generated-date-test-passes-today-and-fails-next-week.jsonld"}}