Skip to content

Unit tests flake on main: two different projects failed and passed on re-run within a day, with no local reproduction #753

Closed

Description

@thinkclay

The Unit tests job on main has failed twice in one day on two different projects, each time passing on a plain re-run of the same commit, with the same commit green locally.

commit project that failed re-run local at that commit
ac49dbd07 (#746) scf-core:test success 1151 passed / 0 failed
a1813d53a (#750) scaffald-app:test success 84 passed / 0 failed

Two different projects rules out a specific bad test, which is what makes this worth filing rather than re-running and moving on.

Why it is hard to see

scf-core:test and scaffald-app:test both write their results to tests/reports/coverage/test-results.json rather than stdout, so the CI log shows only:

NX  Running target test for 5 projects failed
Failed tasks:
- scaffald-app:test

with no failing test name, no assertion, and no stack. Diagnosing it means reproducing locally — and it does not reproduce locally.

Leading suspicion, unconfirmed

Resource exhaustion on the runner rather than a logic fault. The root vitest config already carries scars from this: maxWorkers: 2 with a comment about OOM on 14-CPU machines, maxConcurrency: 3, pool: "threads", and sequence.concurrent: false. NODE_OPTIONS=--max-old-space-size=4096 is set for test:unit. Related in spirit to #680, where the Expo dev server OOMs mid-suite and the harness reports it as app failures.

I have not confirmed it — that is the point of filing rather than guessing.

Why it matters

A gate that fails for environmental reasons stops carrying signal, and the habit of re-running forms quickly. That is the same failure mode as #692 and #710, and it is how a real break gets waved through.

Acceptance

  • The nx test target surfaces the failing test name in the CI log, so a flake can be told from a real failure without a local repro. The JSON reporter is useful; it should not be the only output.
  • Either the flake is reproduced and fixed, or the runner constraint is identified and the concurrency/memory settings adjusted to match it
  • If it is genuinely environmental, a retry with a distinct annotation — so "flaked once, passed" is visible as such rather than indistinguishable from green

Reproducing the observation

Neither instance reproduced locally. Both were re-run with gh run rerun <id> --failed and went green with no code change.

Activity

  1. added
    bug
    on Sep 11, 2026
  2. thinkclay commented on Sep 14, 2026

    @thinkclay
    ContributorAuthor

    #757 is merged. Acceptance box 1 is done; boxes 2 and 3 are not, and I want to be precise about why.

    What the investigation actually found

    The premise of this issue was slightly wrong. I wrote that scf-core:test and scaffald-app:test "failed", and that the log showed nothing useful because those projects write results to JSON rather than stdout. The second half was right for the wrong reason.

    The reporting step was broken, not quiet. pnpm test:health runs after the tests with if: always(), and it has printed this on every run since it was written — green ones included:

    Total Tests: undefined
    Passed: undefined ✅
    Total Duration: NaNs
    ⚠️  No test results found. Run tests first.
    

    scripts/test-health-analyzer.ts parsed { totalTests, passed, failed, tests[] }. Vitest's json reporter emits the Jest shape — { numTotalTests, numPassedTests, testResults[].assertionResults[] }. Every field was undefined, so it bailed early and reported success while saying nothing.

    And five projects were writing one file. Every package inherits the root vitest config, so nx affected -t test had all five writing tests/reports/coverage/test-results.json in turn — including when an earlier project was the one that failed. Measured with a deliberate failing test in scf-schemas and scaffald-app running after it:

    files analyzer reports
    old fixed path 1 85 tests, 0 failed
    one file per project 2 143 tests, 1 failed

    The failure was simply gone. So even if the analyzer had worked, it would have reported on whichever project happened to finish last.

    Both are fixed. In CI on the PR, the same step now prints 2040 tests, 1980 passed, 60 skipped, 78.48s — and a failing test is named with its file and assertion.

    Why boxes 2 and 3 are still open

    I could not reproduce either flake, and I now know I cannot investigate them retrospectively either: GitHub discards a superseded attempt's logs. gh run view --job=<attempt-1 job id> --log returns the re-run's output while the steps API reports attempt 1's conclusions — the two disagree, and I briefly drew a wrong conclusion from it before noticing the timestamps did not match. Both flakes were re-run before their logs were captured, so the only surviving evidence is the failing project name I noted live at the time.

    My resource-exhaustion suspicion is still just that. I have not confirmed it and did not tune maxWorkers/maxConcurrency on a guess — changing concurrency settings to chase an unreproduced fault would most likely just change the timing and hide it.

    Suggest leaving this open until a third flake occurs. That one will say what failed, and that is the input needed to decide between a genuine flaky test and a runner constraint.

    One thing worth doing in the meantime, if anyone hits it: capture the log before re-running. gh run view --job=<id> --log > flake.log takes a second and is the only chance.

  3. thinkclay commented on Sep 15, 2026

    @thinkclay
    ContributorAuthor

    Status check, no change in recommendation.

    Unit tests on main across the last eight CI runs:

    09-15T02:30  1bdcc0dfd  success
    09-15T01:34  aa51ce367  success
    09-15T01:30  87926ba20  cancelled (superseded)
    09-15T00:58  1488e5037  success
    09-15T00:48  5fa50e79c  success
    09-15T00:48  386aca561  cancelled (superseded)
    09-14T23:12  584812017  success
    09-14T22:49  15d40ec1a  success
    

    Six real runs, six passes; the two cancellations are concurrency, not flakes. No third occurrence, so this stays open and unworked per the earlier note — and the same caveat still applies: capture the log before re-running, because gh returns the re-run's output while the steps API reports the original attempt's conclusions.

  4. thinkclay commented on Sep 17, 2026

    @thinkclay
    ContributorAuthor

    Third occurrence, and the first one where #757's reporting names the test — so this is no longer "two projects failed, unknown which tests".

    On #780 (fix/777-circular-deps, a change to a shell script, package.json and a barrel file — nothing that can reach an onboarding form):

    ❌ Failed Tests (1):
      1. GeneralInfoStep keeps continue disabled until required fields are complete
         .../__tests__/GeneralInfoStep.test.tsx
         AssertionError: expected { Object (data, isValid, ...) } to match object
           { data: { …(4) }, isValid: true, …(1) }
    

    Captured before re-running, per the note above. Then:

    • Locally, on the same branch: 3 runs, 3 passes, 2/2 tests each.
    • CI re-run of the same commit: success.

    So the signature is the same as the original report — fails once, passes on re-run, on a commit that cannot have caused it — but now it has a name and an assertion. isValid: true expected against an object that has data, isValid and one more key suggests the step's validation resolves asynchronously and the assertion occasionally reads it a tick early, rather than anything to do with the runner.

    That is a concrete lead for box 2, and it is cheap to test: the spec is packages/scf-core/features/profile-wizard/components/steps/__tests__/GeneralInfoStep.test.tsx, and the question is whether the assertion is wrapped in something that waits for the form state to settle.

    Still not fixing it here — this is out of scope for #780 and I would rather it be someone's deliberate 20 minutes than a drive-by. But the "wait for a third flake" condition from my earlier comment is now met, and it arrived with enough detail to act on.

  5. thinkclay commented on Sep 17, 2026

    @thinkclay
    ContributorAuthor

    Another instance, with the test name this time — the Test health step from #757 did its job:

    • run 35197594403 on fix(a11y): landing and contact pages score 1.0 — real radiogroup/tablist semantics, and a consent banner that is not a stray h6 #792 (a change confined to two marketing components, the cookie banner in @scaffald/ui, and .lighthouserc.json): scf-core:test failed 1 of 1954 — GeneralInfoStep keeps continue disabled until required fields are complete (packages/scf-core/features/profile-wizard/components/steps/__tests__/GeneralInfoStep.test.tsx), AssertionError: expected { Object (data, isValid, …) } to match object ….
    • Locally at the same commit: 3 of 3 passes.
    • The assertion reads onStepStateChange.mock.calls[last] after a waitFor on the button becoming enabled. The button enables on the first valid snapshot; whether the trimmed snapshot the test expects is already the last call at that instant depends on how many state updates have flushed — on a loaded runner it sometimes is not. That is a race in the test, not the component.

    Cheap fix for that one: assert with waitFor(() => expect(onStepStateChange).toHaveBeenLastCalledWith(expect.objectContaining(…))) so it waits for the snapshot rather than the button. Re-ran the job (gh run rerun --failed) rather than doing that inside an unrelated PR.

  6. thinkclay commented on Sep 17, 2026

    @thinkclay
    ContributorAuthor

    Second instance tonight, different test, same shape — logged so the pattern is visible:

    That test deliberately drives a render loop to prove the guard is not vacuous; under a loaded runner the loop's error surfaces as a stack-trace-limit error instead of the assertion the test expects. Worth capping the loop (a bounded render count) rather than relying on React's max-update-depth timing.

  7. thinkclay commented on Sep 22, 2026

    @thinkclay
    ContributorAuthor

    Another data point, 2026-09-21. Unit tests failed on PR #872 and passed on a re-run of the same commit with no changes.

    • Run 35663349950, job Unit tests: fail (2m40s), then pass (2m55s) on re-run.
    • Failing test: apps/scaffald/tests/results-view-xp.test.tsx → results view XP › the old shape really did loop — the guard above is not vacuous.
    • The branch does not touch that file (git diff --name-only origin/main...HEAD has no hit), and nx run scaffald:test passes locally.

    That is a third project added to the two this issue already names, and the same signature: no local reproduction, green on re-run. The test itself is a deliberate negative control — it asserts the old shape loops, so it renders something designed to re-render until a guard trips. A test whose passing condition is "this ran away" is an obvious candidate for timing sensitivity on a shared runner.

    Worth checking whether results-view-xp.test.tsx:122 and profile-view-tracking.test.tsx:126 (the same test name, same pattern) both depend on a render-count threshold that a slow runner can miss.

  8. thinkclay commented on Sep 23, 2026

    @thinkclay
    ContributorAuthor

    Another instance, same shape.

    PR #887 — "Unit tests" failed on scaffald-app:test (run 35897507126). The job log shows every other project passing (sdk 778/778, scf-core clean) and scaffald-app:test failing with no assertion output — the only signal in the log is the usual [MSW] Error: intercepted a request without a matching request handler.

    Run locally on that exact branch: scaffald-app:test passes, 98 of 99 with one skipped, nothing failed. Re-queued the failed job.

    That makes at least three occurrences now, and each time the failing project's own output is missing from the log while a neighbouring MSW warning is present. Whatever the flake is, the missing per-test output is the thing making it hard to pin — a reporter that always writes the failing assertion would turn this from "re-run and hope" into something diagnosable.

  9. thinkclay commented on Sep 25, 2026

    @thinkclay
    ContributorAuthor

    Another sighting, with a bit more detail on the conditions.

    While working #839 locally I ran scf-core:test with the Expo dev server and a Playwright browser running alongside it. Two suites failed:

    • packages/scf-core/features/discover/components/__tests__/MapSearchInput.test.tsx — "renders search input and calls handlers when token valid"
    • packages/scf-core/features/profile-wizard/components/__tests__/ProgressIndicator.test.tsx — "renders current step and total steps"

    Both reported Error: STACK_TRACE_ERROR with a stack entirely inside @vitest/runner, and no assertion message — the same empty-failure signature this issue describes.

    Both pass when run alone (npx vitest run -t "renders search input and calls handlers when token valid" → 1 passed). Stopping the dev server and re-running the full suite returned 1236 passed / 0 failed / 0 failed suites, matching the baseline.

    Neither file was touched by that branch. The distinguishing detail versus previous sightings: this one reproduced under memory/CPU pressure from a concurrently running dev server, which suggests the flake is a resource-exhaustion path in the worker pool rather than anything in the tests themselves. Related to the vitest pool config notes in .radium/ and the Metro OOM behaviour recorded elsewhere.

  10. thinkclay commented on Sep 25, 2026

    @thinkclay
    ContributorAuthor

    Two more instances tonight, both the same test shape rather than the same test — which points at a design problem, not ambient flakiness.

    PR Failing test File
    #908 profile view tracking > the old shape really did loop — the guard above is not vacuous apps/scaffald/tests/profile-view-tracking.test.tsx:126
    #870 results view XP > the old shape really did loop — the guard above is not vacuous apps/scaffald/tests/results-view-xp.test.tsx:122

    Both failed with Error: STACK_TRACE_ERROR, both passed on a plain re-run with no code change, and #908's passed 3/3 locally on the failing branch.

    They are the same construction:

    it('the old shape really did loop — the guard above is not vacuous', async () => {
      render(<Looping … />)
      await settle(120)
      expect(onCall.mock.calls.length).toBeGreaterThan(20)
    })

    Each deliberately renders the pre-fix component so that it re-renders without bound, then asserts it got more than 20 iterations into a 120 ms window. The intent is good — a guard test that cannot fail is worth nothing, and this proves the guard above it is not vacuous. But the mechanism is deliberately inducing an unbounded loop and racing a wall-clock window, which under a loaded CI runner overruns into a stack overflow instead of stopping at 20-odd iterations. STACK_TRACE_ERROR is the test succeeding too hard.

    So the flake is not random: it is these two tests, and it will keep recurring. Worth bounding the loop rather than timing it — cap the render count and assert the cap was reached, so the test proves the same thing without depending on how fast the runner is.

    Why they surface on dependency PRs specifically: CI runs Test (affected), and a lockfile change makes every project affected. So a Dependabot or deps PR runs apps/scaffald's suite when an ordinary feature PR does not — which is why both sightings tonight were on dep PRs, and why each initially read as a regression from the bump. That cost real time on #908 before I re-ran it.

  11. thinkclay commented on Sep 26, 2026

    @thinkclay
    ContributorAuthor

    Fix for the apps/scaffald half is up in #915.

    Measured the loop before changing it, which reframed the problem:

    window=60ms  calls=6143
    …then: Error: Worker exited unexpectedly
    

    6,143 iterations in 60 ms. So toBeGreaterThan(20) was never at risk of being unmet — the runaway was the failure. Waiting a fixed 120 ms while an unbounded render loop runs is enough to kill the vitest worker, which surfaces as STACK_TRACE_ERROR against whichever test was in flight. That explains the "no local reproduction" note in the original report: it depends on machine speed and load, and the error names the wrong test.

    #915 replaces the wall-clock window with a counted bound — stop the loop at 25 iterations and resolve — so the same fact is established without racing anything. Both cases now run in ~11 ms instead of 120 ms+, and the pair passed 6/6 consecutive runs.

    The guard is still a guard: verified by mutation that removing the loop makes it fail with expected 'stalled' to be 'looped' rather than passing vacuously.

    Not closing this. The issue was filed as two different projects flaking, and I have only accounted for apps/scaffald. Worth leaving open until a clean stretch goes by — if something in scf-core or packages/ui flakes again, that is a separate cause and this issue is where it belongs.

  12. thinkclay commented on Sep 26, 2026

    @thinkclay
    ContributorAuthor

    Closing — fixed in #915 (merged).

    What this was

    Both sightings were the same construction, in two different suites:

    PR Test
    #908 profile view tracking > the old shape really did loop — the guard above is not vacuous
    #870 results view XP > the old shape really did loop — the guard above is not vacuous

    Each deliberately rendered the pre-fix component so its effect re-entered itself unbounded, waited a fixed 120 ms, then asserted it had got past 20 iterations. Measured:

    window=60ms  calls=6143
    …then: Error: Worker exited unexpectedly
    

    6,143 iterations in 60 ms. toBeGreaterThan(20) was never at risk of being unmet — the runaway was the failure. The worker dies and vitest reports STACK_TRACE_ERROR against whichever test was in flight, which is why this looked random and why the original report found no local reproduction: it turns on machine speed and load, and the error names the wrong test.

    #915 replaced the wall-clock window with a counted bound — stop the loop at 25 iterations and resolve. Same fact established, nothing raced: 120 ms+ → ~11 ms, 6/6 across consecutive runs. The guard still guards, verified by mutation (removing the loop makes it fail with expected 'stalled' to be 'looped' rather than passing vacuously).

    Two things worth keeping

    Why it surfaced on dependency PRs. CI runs Test (affected), and a lockfile change makes every project affected — so apps/scaffald's suite runs when an ordinary feature PR would not. Both sightings were on dep PRs, and each first read as a regression from the bump.

    The scope caveat. This issue was filed as "two different projects failed and passed on re-run within a day", and #915 accounts for the apps/scaffald half specifically. I flagged that I could not prove these two tests were the only cause. Closing on Clay's call — so if something in scf-core or packages/ui flakes again, that is a new issue rather than a reopen, and this one is the prior art to link.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions