DEV Community

Cover image for Silent Failure #21: The Suite Passed Because Nine Files Never Loaded
Renhua Wei
Renhua Wei

Posted on

Silent Failure #21: The Suite Passed Because Nine Files Never Loaded

Seventeen test files. Nine of them never loaded. The runner printed ALL PASSED and exited 1, and I only noticed the second part four hours later.

I was in taurheim/scrobblify, on a branch adding a filter for placeholder rows, and before touching anything I wanted a baseline to quote in the PR body. Standard move:

$ npm test 2>&1 | tail -20
...
ALL PASSED
Enter fullscreen mode Exit fullscreen mode

Reads like a pass. I wrote RC=0 in my notes and moved on to the actual fix. That number went into my head as a fact, and facts you write down early are the hardest ones to question later.

The thing I did to myself is small and embarrassing: I piped the test command into tail. cmd | tail returns tail's status, so the runner's exit code never reached my shell in the first place. I have this rule written down. I wrote it into the runbook last week after the openssl cert that verified fine with the wrong name. I applied it there, felt good about it, and then typed | tail here anyway, because this was "just a baseline". Silly mistake, and it cost me the afternoon.

So I ran it again the boring way:

$ npm test > /tmp/t.log 2>&1; echo RC=$?
RC=1
Enter fullscreen mode Exit fullscreen mode

Green tail, red exit. My first guess was a flaky test — there's one timing-sensitive case in this repo and I have blamed it before, wrongly. I re-ran twice. RC=1, twice, with ALL PASSED sitting at the bottom of the log both times.

Then I did the thing I should have done first, which is to stop reading the log and count it.

$ grep -c "^ALL PASSED" /tmp/t.log
14
$ ls tests/*.mjs tests/*.test.* 2>/dev/null | wc -l
17
Enter fullscreen mode Exit fullscreen mode

Fourteen pass lines for seventeen files on disk. The log wasn't lying about the passes. It was describing a smaller world than the one I live in.

Nine files in that tree abort at their first import:

Error [ERR_UNKNOWN_BUILTIN_MODULE]: No such built-in module: node:sqlite
    at node:internal/modules/esm/loader:...
Enter fullscreen mode Exit fullscreen mode

node -v on the box: 20.18.1. node:sqlite landed in 22.5. The nine are api, blobs, chunks, export-queries, handoff, housekeeping, jobsize, scheduler, store — everything that touches the store, which is most of what the product does. They never reach a single assertion. They die in the loader, and scripts/run-tests.mjs does this with them:

try {
  const mod = await import(file);
  ...run
} catch (e) {
  failures++;          // just a number
}
Enter fullscreen mode Exit fullscreen mode

A counter. Not the filename, not the stack, not even a red line. The catch swallows the loader error, bumps an integer, and the summary at the end prints ALL PASSED if the cases that actually got collected all passed. Which they did. Eight files' worth of cases, all green, cheerfully summarised as the whole suite.

I would have shipped that as a baseline. That's the part that keeps me up: a green baseline is what I compare against. If eight of seventeen files are invisible on the baseline run, then my "no regressions" claim the next morning covers exactly those eight files, and the number I was proud of means nothing.

The fix that stuck was not "print the failure". It was realizing that a runner doesn't know how many test files exist. It only knows how many it was handed. So the gate has to supply the second number from outside:

$ npm test > /tmp/t.log 2>&1; RC=$?
$ FILES=$(find tests -name '*.test.*' | wc -l)
$ PASSED=$(grep -c '^ALL PASSED' /tmp/t.log)
$ echo "rc=$RC files=$FILES passed=$PASSED"
rc=0 files=17 passed=14
Enter fullscreen mode Exit fullscreen mode

I installed Node 22.23.5 rather than the distro 20.18.1, and the same command came back rc=0 files=17 passed=14. Still three short — three of those files print their own summary instead of ALL PASSED, which I only know because I went and looked. The point isn't that 14 is the right number. The point is that on the red run the gap was 5 and on the green run it's 3, and both numbers are now in the log every single time, so the next person can't be fooled by the tail the way I was.

If you want the invariant in one line instead of two: the runner should exit non-zero when collected < discovered, and it should print the filename it failed to import. Both are inside run-tests.mjs's reach — it walks the directory already, it just throws the count away. I'd argue that swallowing a loader error is worse than failing loudly, because a broken import is not a test result at all. It's the absence of one, wearing a pass's clothes.

Two things I'd put in the CI job rather than in a doc:

  • the count of collected files, printed unconditionally, next to the count on disk
  • a minimum: refuse to run the summary if passed < files

passed=14 files=17 is a line you can alert on. ALL PASSED is a line you can only believe.

I've only measured this on one repo with a hand-rolled run-tests.mjs and a distro Node, so my sample is thin; the pattern is what I'd bet on, not the exact numbers.

For anyone on Jest 29 or Vitest 2 with a Node 20 image: when a file imports a node: builtin that version doesn't have, does your runner name the file and exit non-zero, or hand you fewer results and a green summary? I want the case at 100+ files where collection dropped exactly one, because that one never gets found by hand.

My take, and I'll take the argument: a runner that prints a blanket pass without printing how many files it actually loaded is a footgun, and it's the default footgun in every zero-config runner I've used. Coverage doesn't save you — the nine dead files contribute nothing to it, so the denominator shrinks with them and the percentage stays beautiful. 91% on a run where the store module was never parsed. If you think a runner should stay quiet about files it can't load, tell me the case where that saved you. Comments are open.

Top comments (3)

Collapse
 
userxnom profile image
Abdullah Naseer •

Renhua, the part that stuck with me is that this was two silent failures stacked on each other — the | tail pipe ate the exit code, and the runner's catch ate the nine missing files. Either one on its own would probably have surfaced; together they turned eight files' worth of tests into a green baseline.

Your collected-vs-discovered invariant is the right fix, and the one I'd add to it is making the runner emit that count in a form CI can assert on without grepping — even a tiny JSON manifest (discovered, collected, passed, failed, plus the filenames that failed to import) turns 'ALL PASSED' from a line you believe into a line you can alert on. And for the pipe half of it, set -o pipefail in the shell script would have made npm test | tail -20 fail loudly on day one, which is cheap insurance for any command you pipe for readability.

One question on the Node side: now that you're on 22.23.5, are you pinning that version anywhere the next person (or the next CI image) would see it — a .nvmrc or an engines field — so a fresh checkout on a distro Node 20 can't silently reproduce the same nine missing files?

Collapse
 
dafili profile image
Renhua Wei •

Not pinned: 22.23.5 on my box is a tarball I unpacked by hand, so a fresh clone on the distro's 20.18.1 drops straight back into the same nine missing files. And pipefail is less universal than it reads — my Makefile shells out to sh -c "npm test | tail -20", where the flag dies with the subshell before npm's status ever escapes. Exact patch in .nvmrc plus engines is the only version of this that survives a new CI image; do you pin the patch or just the major line?

Collapse
 
userxnom profile image
Abdullah Naseer •

That sh -c detail is a great catch — you are right, pipefail set in the Makefile's shell never reaches the subshell, so the flag was doing nothing there. The fix I would actually use is putting set -o pipefail inside the sh -c string itself, or skipping the pipe entirely and writing the output to a file, then tailing the file — that way the exit code never passes through a pipe at all.

On pinning: I pin the exact patch, 22.23.5 in .nvmrc plus the same in engines. A major line still lets a fresh install drift across patches, and patch releases have changed small behaviors on me before, so the exact patch is the only version that makes "works on my box" reproducible. Cheap insurance, same as the invariant.

Out of curiosity — now that the .nvmrc plus engines pair is in, are you planning to add a CI step that fails when the discovered and collected counts disagree, or is that invariant staying as a local check for now?