Skip to content

test(frontend): guard unit tests against real network calls - #411

Merged
haksungjang merged 1 commit into
mainfrom
fix-267-frontend-test-network-guard
Sep 6, 2026
Merged

test(frontend): guard unit tests against real network calls#411
haksungjang merged 1 commit into
mainfrom
fix-267-frontend-test-network-guard

Conversation

@haksungjang

@haksungjang haksungjang commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Summary

Issue #267 reported the frontend unit suite flaking under CI load with a
different test failing each time, and neither failure reproduced locally.
tests/setup.ts never blocked fetch or XMLHttpRequest, so a request that
slipped past a mock could reach the real network (or hang on a host that
doesn't exist in CI) instead of failing the test that made the call — the
resulting error then surfaced later, in whatever test happened to be running
at the time.

This adds a network guard to tests/setup.ts: fetch and XMLHttpRequest
are both replaced with a stub that throws immediately, naming the current
test (via expect.getState().currentTestName) and the request URL. A test
that intentionally needs the real implementation opts in with
allowNetworkInThisTest(), which the guard re-arms on every afterEach so
the opt-out can't leak into the next test.

Ran the full 235-file / 2624-test suite three times with the guard active;
all green every time. That means the guard did its job at making leaks
attributable, but found none currently in the suite — every test file is
already mocking its network calls correctly. That explains and closes the
fetch/AggregateError symptom from the original report (it's now a
structurally impossible failure mode). The data-testid="policy-exception-row"
symptom from the same report is a different root cause (looks like Radix
portal/focus-trap timing or worker-level resource contention, not a network
leak) and is left out of scope for this PR; noted on the issue.

Verification

  • node node_modules/vitest/vitest.mjs run (Node 20, full suite): 235/235
    files, 2624/2624 tests green, run three times back to back.
  • Verified the guard's error message by temporarily calling fetch(...) and
    opening new XMLHttpRequest().open(...) inside a scratch test: the thrown
    error contained both the offending URL and the full current test name, e.g.
    Unmocked fetch call to "http://example.com/probe-2" during test "network guard temp probe > captures the test name and url in the thrown error".
    The scratch test file was removed before this commit.
  • tsc -b --noEmit: clean.
  • eslint tests/setup.ts: clean.
  • node scripts/i18n-check.cjs: OK (pre-existing dynamic-key warnings only,
    unrelated to this change).
  • node tools/em-dash/lint.mjs: OK.

Closes #267

Nothing in tests/setup.ts blocked fetch or XMLHttpRequest, so a request
that slipped past a mock could reach the real network instead of
failing the test that made it, and whatever landed late surfaced as
noise in a random other test (issue #267).

Replace both with a stub that throws immediately, naming the current
test and the request URL. A test that intentionally needs a real
network call opts in via allowNetworkInThisTest(), re-armed every
afterEach. Ran the full 235-file suite three times with the guard in
place; all pass, so the guard finds no existing leaks.
@haksungjang
haksungjang force-pushed the fix-267-frontend-test-network-guard branch from 5ca445f to 2f2ab2d Compare September 6, 2026 15:57
@haksungjang
haksungjang merged commit 56a5eb5 into main Sep 6, 2026
25 checks passed
@haksungjang
haksungjang deleted the fix-267-frontend-test-network-guard branch September 6, 2026 21:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Frontend unit test suite flakes under CI load (random test fails, not the same one twice)

1 participant