When Tests Lie
Tests that fail for the wrong reason, pass for the wrong reason, or only fail somewhere else. Real examples from this site and how each was fixed.
Trust is the whole value
A test suite is useful only if a red result means "something is broken" and a green one means "it is safe". Every false alarm teaches people to ignore red. Every false pass teaches them to trust green too much. Here are the ways tests have lied on this site.
1. The number that grows by itself
A Ravi OS test checked a count of items waiting to be processed, and wrote the expected number into the test. The count grew by one every day, as it was supposed to. One night the test turned red on main although nothing was broken.
Fix: test the rule, not today's value. "The count is a whole number, at least as large as yesterday's, and the list has that many items" stays true forever. The fix landed four minutes after the failure, and it is one of only two failures in the workflow's history.
Lesson: never hard-code anything that changes with time or content: counts, dates, "the newest post".
2. The tool that was not there
Some tests read the Git history (to show recent commits in Ravi OS). They passed on the laptop and in GitHub Actions. In Cloud Build, they ran inside a slim Node image with no Git installed, failed, and stopped the deploy. Worse, nobody noticed for a while: several merges in a row were never deployed.
Fix: tests that need Git now skip, with a clear message, when Git is missing, and a test checks that they skip instead of failing.
Lessons: CI is not one environment; it is several. And a failure that nobody sees is the most dangerous kind: see Alerts That Reach You for how that gap is closed.
3. The binary built for another system
A test that processes images uses sharp, a library with native code compiled for each operating system. Run on Linux with packages installed on Windows, it fails with "could not load the sharp module for linux-x64". The code was fine; the environment was mixed.
Fix: always install dependencies on the machine that runs them. npm ci in CI does this. When copying a project between systems, delete node_modules and install again.
4. The test that passes against the wrong copy
A test that checks "the current state of the site" can pass while reading an old file left on disk. An untracked script, a stale build output or a local backup can make a check pass locally that fails on a clean checkout, or the other way round. Here a local run once failed because two picture scripts sat in scripts/ without an entry in the README: true locally, irrelevant to CI, which never saw those files.
Fix: treat a failure that only happens locally as a sign of local clutter, and clean the clutter rather than the test.
5. Noise that hides signal
Some tests send deliberately broken input to check it is refused. The refusal is correct, but the library still prints a long error trace into the test output. A reader skimming the log sees a scary stack trace in the middle of a green run.
Fix: silence expected errors inside the test, so that any trace you see in the output is a real one.
A checklist for an honest suite
- No values that change with time or content.
- No dependence on tools that are not guaranteed everywhere the tests run.
- Dependencies installed on the machine that runs them.
- A failure that happens in only one place is investigated, not retried until green.
- Expected errors are kept out of the output.
- Every fixed bug gets a test, and every flaky test gets fixed or deleted, never ignored.
Try it yourself
Search your tests for literal dates, counts and "latest" items. Each one is a future false alarm. Rewrite one as a rule about the data instead of a snapshot of it.