[PR #198·199·200] Three Tests That Failed Now and Then: The Cost the First Test Carries Alone, and the Cost of Building a Failure Message
I merge after running full verification (npm run verify) on my Windows computer. So even one test that fails now and then blocks merging. While another PR was being checked, three tests each failed once, and the sessions doing those checks handed each fix off separately. All three PRs were done by Claude Code (Claude Opus 5.5) on my Windows computer, and in none of the three cases was it a bug in the app code.
- What was done:
- PR 198: raised the limit for waiting for the first table in the admin web date table test.
- PR 199: raised the time limit only for the first test of the customer app bottom menu.
- PR 200: made the check that the customer app home gauge has disappeared lighter.
The three tests side by side:
| Item | PR 198 (admin web date table) | PR 199 (customer app bottom menu) | PR 200 (customer app home gauge) |
|---|---|---|---|
| Limit hit | waitFor default 1 s |
5 s per test | 5 s per test |
| Cause | The file’s first test carries the first-render cost alone | The file’s first test carries the library’s first-render cost alone | A failed check spends 0.45 s building its failure message |
| Fix | 3 s limit on the first waitFor only |
15 s time limit on this test only | Check that “the count found is 0” |
PR 198: Admin web date table
- Cause: The test was already using
waitFor(a tool that keeps checking until a condition holds) to wait for the table to finish drawing. This test is the first in its file to draw the full table, so it carries the cost of the first fake response and first render alone. When all admin tests ran together, this one took 0.7–1.3 seconds, while the rest stayed under 0.5 seconds. The defaultwaitForlimit is 1 second, so on runs past 1 second it ended with the table still “loading”. Before the fix it actually reproduced in 2 of 3 runs. - Method chosen: a 3-second limit on only this test’s first
waitFor, with the reason left in a comment. - Alternatives dropped: raising the
waitForlimit for all admin tests (a truly slow screen would be noticed late, and the change is broad), and cutting the workers further (already halved, and all tests would get slower). - Check: after the fix, this test passed in all 15 full admin test runs (slowest 1.26 seconds, a time that would have failed before). In one of those runs a different test, whose name was not captured, failed once; the next 10 runs all passed.
PR 199: Customer app bottom menu
- Cause: When Claude measured step-by-step times inside the test, drawing the app (
renderRouter) took 517 ms in the first test and 52–58 ms in the other 8. The first test carries alone the cost of loading and drawing the router (the part that handles moving between screens) and the screen code for the first time. Loading the screen files in advance only cut 517 ms to 429 ms, so most of the cost was the library’s first render. - Why the failure message said “5000 ms”:
renderRouterturns on a fake clock (a clock that imitates time for tests), so the 1-second wait was measured in fake time and never hit, and only the 5-second limit, measured in real time, was hit. - Reproduction: with 48 processes doing other work to keep the CPU busy, the test before the fix failed with the same message at 7.9 and 8.8 seconds, and the fixed test passed at 9.9 seconds.
- Method chosen: a 15-second time limit on this one test, with the reason left in a comment. What the test checks is unchanged.
- Alternatives dropped: loading the screen files before the test group starts (barely cut the first-render cost), drawing once in advance (router state could carry into the first test, and the code grows), and raising the time limit for all customer app tests (a truly slow test would be noticed late).
- Check: the fixed file passed all 5 runs on its own, and it passed in all 3 full
npm run verifyruns.
PR 200: Customer app home gauge
- Cause: When Claude measured step-by-step times with a real clock, drawing the app took 40 ms, but “waiting until the gauge disappears” took 457–469 ms. Breaking it down further showed the time went to the first check failing.
- The first check always fails. The reloaded data reaches the screen through a timer, and with the fake clock it is drawn only after
waitFormoves the clock forward once. The app is not behaving wrongly. - A failed
expect(screen element).toBeNull()turns the value it received into text to build its failure message. A screen element links to a long chain of React internal objects, so turning it into text takes about 0.45 seconds each time. This is CPU work, so it grows with load.
- The first check always fails. The reloaded data reaches the screen through a timer, and with the fake clock it is drawn only after
Here is what each check style cost when made to fail on purpose.
| Check style | Time per failure |
|---|---|
expect(queryByLabelText(..)).toBeNull() (before) |
454 ms |
expect(queryAllByLabelText(..)).toHaveLength(0) |
136 ms |
expect(queryAllByLabelText(..).length).toBe(0) (after) |
9 ms |
expect(..).not.toBeOnTheScreen() |
9 ms |
- Method chosen: check that the gauge disappeared as “the count found is 0”. It checks the same thing, and on failure it prints only a number, so it is light. One line in one file was changed.
- Alternatives dropped: the dedicated check
not.toBeOnTheScreen()(it runs, but this project’s type checking does not know it, so another type declaration file would need changing), and raising the time limit as in PR 199 (the cause would stay, and every run would keep spending 0.45 seconds). - Check: with 32 processes keeping the CPU busy and the whole file running, this test took 3865 ms and 4115 ms before the fix (just under the 5-second limit) and 685 ms and 506 ms after. Without load it took 107–126 ms (about 530 ms before), and 2 full
npm run verifyruns both passed. - Not used as evidence: running only this test makes it the first test in the file, so it also carries PR 199’s first-render cost. That differs from the real verification order, so Claude did not use that result as evidence.
Lessons
- “Passes alone, sometimes fails in a full run” is usually a file’s first test hitting a limit. Run everything a few times with test times shown and it shows up quickly.
- To see whether a test is “just under the limit”, keep the CPU busy on purpose and run it. The occasional failure reproduces right away.
- In tests that turn on a fake clock, the time-measuring functions are fake too.
jest.requireActual('perf_hooks').performance.now()gives real time. - When waiting for something to “disappear”, compare a count instead of a screen element. When a check that received a screen element fails, building the message alone costs hundreds of ms.