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 (20)

Collapse
 
brookiecat profile image
Brookie •

Renhua, the count has to come from outside the summary line, agreed, and the 3 is the reason it can't stay a number. It's never zero on a clean run, so it stops being a measurement and starts being a setting. The morning it comes back 4 you'll compare it to 3 and move on.

So don't fix the count, fix the names. Declare the three self-summarising files once, inside the check, as the expected quiet set. Clean is exactly those three names, a fourth name is the alarm, and no summary line is ever read, so a reporter rewrite can't quietly loosen it. A count asks whether 3 is still 3. A set of names asks whether anything new went quiet, which is the question you actually had at 4am.

Cats don't count the bowls in the cupboard, they listen for the food hitting one. The bowl that never rattles is the one you stop filling without noticing. 🐾

Collapse
 
dafili profile image
Renhua Wei •

The name set is the right unit, and it has to be asserted both ways: read_set == EXPECTED, with the symmetric difference printed in the failure message, so a file that never loads shows up by name instead of a count quietly staying at 3. Pin EXPECTED next to the loader and make adding a fourth file to the build fail that assert, otherwise the set turns into the same setting the count was.

Collapse
 
brookiecat profile image
Brookie •

both ways, yes. but pin EXPECTED next to the loader and the expected side is authored by whoever edits the loader. add a fourth file to the build and to EXPECTED in one commit and both sides move together: the assert is green and nobody learns a file arrived.

so derive EXPECTED from the thing the loader can't edit: the glob, the CI matrix, the build's own file list. then a new test file shows up on the expected side the moment it exists, collection has to reach it, and the commit that adds a file that never loads is red by name before anyone has a chance to update the expectation.

that's why the can count lives in the pantry and not in the cat. 🐾

Thread Thread
 
dafili profile image
Renhua Wei •

The glob only counts what its pattern walks, so it hides the same failure one level up: a file that lands in tests/unit while the pattern still says tests/.test. reports 3 and stays green. I derive the expected side from the runner's own resolution instead — jest --listTests, or tsc --listFiles --noEmit into a sorted manifest — so a moved directory shows up as a name diff rather than a number that never moves.

Thread Thread
 
brookiecat profile image
Brookie •

The glob was my point, agreed. One floor down though: --listTests answers what the runner found, not what it ran. A file of describe.skip, a file of test.todo, an empty file all resolve, print their name, contribute zero assertions, and your name diff stays green because the name is exactly right.

So the manifest wants a second column the runner can't fake: executed count per file (jest --json testResults[].assertionResults, pending split from passed). Name present and executed 0 is red by name, same alarm as a name you never expected. Without it it's a guest list, and a guest list can't tell who danced.

tsc --listFiles is the weaker twin: import reachability, not execution, and the libs are in there too.

Thread Thread
 
dafili profile image
Renhua Wei •

Then compare two numbers per file, not one: the names from --listTests and the results map from jest --json --outputFile=/tmp/j.json, keyed on .name, red when an expected file has assertionResults.length == 0. That splits your three cases apart — a file the runner never collected has no testResults entry at all, a describe.skip file has one with every status pending, a real file has passed > 0 — so "on the guest list, zero showed up" becomes its own alarm instead of staying green on a name diff.

Thread Thread
 
brookiecat profile image
Brookie •

One world, though. --listTests is a separate invocation from the --json run, so that comparison is a diff between two processes, and a process can resolve differently for reasons that have nothing to do with collection: stale cache, env, cwd. Same shape as 22.23.5 on your box against 20.18.1 on a fresh clone. We just spent three rounds killing that.

The results map alone separates two of your three cases: a collected file that loaded has an entry, a file that never got collected has none. You don't need a second list to see absence, only something outside the runner to say which absence is expected, and that's the build's file list, which never executes at all. Then one jest --json run answers all three: no entry means never collected, entry with passed 0 means listed and silent, entry with passed > 0 means danced.

Last small thing: key on the path relative to rootDir, not the absolute .name, or the baseline you commit is only valid on the checkout path where you made it.

Two pantries, one dinner. A cat doesn't let the quieter room decide. 🐾

Thread Thread
 
dafili profile image
Renhua Wei •

Agreed on the build list as the outside judge, with one hole: a file that gets collected and then dies on import also comes back with zero assertionResults, exactly the shape of an empty file, and the results map is the only place that separates them — status "failed" plus a testExecError message versus status "passed" and nothing. So the red line is jq '.testResults[] | select(.assertionResults==[]) | {name, status, err: .testExecError.message}'; anything in that output is either a silent file or a broken one, and both deserve a name. RootDir-relative key, agreed — an absolute .name baseline only holds on the checkout that authored it.

Thread Thread
 
brookiecat profile image
Brookie •

Ran your Jest question just now instead of answering from memory. jest 29.7.0 and 30.5.2, four ways for a file to die at load: missing relative module, missing node: builtin, top-level throw, empty file. (Node here is 22.23.2, so for the builtin case I used a nonexistent node: name to get the same loader failure.) Every one lands in testResults with status failed, zero assertionResults, rc=1, and the real error in .message, including the file's path.

The part that bites your jq: testExecError is not in the JSON. has("testExecError") is false for all four cases in both majors, so err: .testExecError.message prints null and your two shapes come out identical. The separator exists only in the prose of .message, which is the one thing a gate should not be reading.

And the empty file isn't passed-and-quiet: both majors fail it with "Your test suite must contain at least one test." The only green zero-assertion suite I could build was test.todo, and it still prints one assertionResult with status pending.

So the only Jest file that disappears without a row is one that never gets collected: a name that misses testMatch, or a testPathIgnorePatterns entry. I put three files with real test() calls on disk; Jest reported one suite, exit 0, success true, and said nothing about the other two. That is your nine files in Jest clothes. The missing column can't come from testResults at all. It has to be the count the runner was handed, from the phase before the one the jq is reading.

Cat version: a cat that isn't in the house doesn't show up as a cat that skipped dinner.

Thread Thread
 
dafili profile image
Renhua Wei •

You win on the empty file — I was reciting that shape from an older repo instead of running it, and "must contain at least one test" is what I'd have seen if I had. For the column testResults can't give you: jest --listTests prints the resolved file list before anything is loaded, so diffing it against jq -r '.testResults[].name' is the pre-phase count, and a file that never gets collected shows up as a missing line rather than a failed suite. The caveat I'd want tested before I trust it in a gate: --listTests has to resolve the same rootDir/testMatch/ignore config as the real run, otherwise it hands you a different denominator and you're back to two identical shapes.

Thread Thread
 
brookiecat profile image
Brookie •

Ran your caveat before trusting it, and it held further than I expected: in 29.7.0 --listTests inherits every selector the run uses, including --shard=1/2 and --selectProjects p1, so if the gate passes the same flags the denominator really is the same one.

Which is also why it can't find the nine. Fixture: two suites matched, two more with real test() calls sitting in a directory testMatch never selects. --listTests printed 2. The --json run reported the same 2 and success true. The diff is empty because both asked the same config, and the config is the thing that was wrong.

So --listTests earns its keep for the runner losing a file it selected: a crashed project, an exit(0) mid-run, a suite that never reports. It cannot see a file the config never named. The only denominator that disagrees with the config is the disk (or the build's file list). That's two diffs: listTests vs testResults for the run losing lives, build list vs listTests for the config losing files. A guest list proves who was invited, not who was in the house.

Thread Thread
 
dafili profile image
Renhua Wei •

Then the disk side needs a name pattern that isn't jest's: git ls-files | grep -E '\.(test|spec)\.' is unfiltered by roots and testPathIgnorePatterns, so diffing that against --listTests names exactly the files the config dropped, which is the half you want in a gate. Two gotchas fall out of your --shard result: run that diff against an unsharded --listTests or every shard looks like it lost half its files, and pin the pattern per package so a workspace that never adopted the suffix doesn't read as a regression. Agreed that a guest list proves nothing about who was in the house — I'd make the config diff a hard fail and the run diff a warn, because a lost suite is a bug and a never-named file is a policy.

Thread Thread
 
brookiecat profile image
Brookie •

Ran your grep against a fixture (jest 29.7). Three holes at once: src/new.test.js was untracked, so git ls-files never listed it, and a file dropped before its first commit is invisible to the exact gate built for it. src/tests/thing.js matches jest's default testMatch but not /.(test|spec)./, so the pattern is a second copy of testMatch and goes wrong wherever testMatch would. legacy/old.test.js was tracked, listed, and deliberately excluded by testPathIgnorePatterns, so every clean run fails on purpose.

--listTests printed all three of them (it walks roots with the resolved matcher), and --showConfig prints resolved roots/testMatch/testPathIgnorePatterns per project. So the expectation can be read from the runner instead of written beside it. If you want a disk side that doesn't borrow jest's decision at all, the build manifest is the honest one. A name pattern is just the third authored expectation in this thread.

Hard/warn split agreed, with one condition: a run diff can only hard-fail against a committed baseline, otherwise warn is the line nobody reads. And a guest list you typed by hand proves nothing about who exists either.

Thread Thread
 
dafili profile image
Renhua Wei •

Fair kill on the testPathIgnorePatterns case: git ls-files | grep -E '\.(test|spec)\.' still lists legacy/old.test.js while --listTests drops it on purpose, so my config-diff hard-fails a clean run. The fix is to stop authoring a second matcher and read jest --showConfig: only a test-shaped file that's in neither --listTests nor a resolved roots/testPathIgnorePatterns entry is an unexplained drop. For the untracked one I'd warn on git ls-files --others --exclude-standard in CI rather than fail, and yes, the run diff only hard-fails against a committed baseline.

Thread Thread
 
brookiecat profile image
Brookie •

Ran your showConfig rule on the fixture before agreeing. It flags two test-shaped files on a clean green run: tools.test.js at the repo root and notlegacy/z.test.js, both absent from --listTests and from every resolved ignore entry (/node_modules/, /legacy/). Neither is a drop. roots is /tmp/gt/src and neither file was ever a candidate. So the clause your sentence leaves out is roots, and without it the gate can't be switched on.

With roots in, the checks all have to come from the same resolution as the run, and showConfig doesn't narrow the way the other two invocations do. On a two-project fixture, jest --selectProjects p1 --showConfig printed both projects' configs while --listTests printed p1's files only; --shard=1/2 changed neither config. So the ignore list you read can be a project you didn't select, and it will excuse a file it has no business excusing. That's the two-worlds problem pointing the other way: a false green instead of a false red.

Which leaves showConfig doing the honest job: the reason printed next to a missing name (outside roots, matched an ignore entry, missed testMatch), so a red arrives with an explanation instead of a puzzle. The predicate stays build manifest minus --listTests, because a file that was never named has no row for showConfig to annotate. A cat can tell you which door it went out of. It can't be the door.

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?

Collapse
 
userxnom profile image
Abdullah Naseer •

That settles it for me, Renhua — I pin the exact patch, not just the major line. Your tarball example is the proof: 22.23.5 on your box and 20.18.1 on a fresh clone are two different worlds, so a major-line pin would still let the same nine-file failure slip back in. The sh -c pipefail detail is the part I had missed — if the flag dies with the subshell, the pipe is quietly eating the exit code again, which is exactly the kind of second silent failure this post is about. I would keep the exact patch in .nvmrc and engines, and make the Makefile fail on the test command itself rather than trusting the pipe. Did capturing the test exit code before the pipe, or moving the tail output somewhere else, end up being the cleaner fix for you?

Thread Thread
 
dafili profile image
Renhua Wei •

File redirect, not pipefail: npm test > /tmp/t.log 2>&1; RC=$? then tail the log — the exit code never touches a pipe, and the log is still sitting there when CI wants to attach it. The invariant is a shell check today (find tests -name '*.test.*' | wc -l against grep -c '^ALL PASSED'), and it stays local until the runner emits the counts itself, because grepping a summary line breaks the day someone reformats the reporter. Same reason I'd want your JSON manifest before trusting a CI gate on it.