Repository navigation
Stop app unit tests from reaching the network - #1504
Merged
Merged
Conversation
The shared Vitest setup now installs a guard as the global fetch and as XMLHttpRequest's send. A request that no test mocked is refused at once with an error naming its method and URL, and a check after each test (and after each file's last test) fails the test that sent it, even when the code under test catches the error. A test that mocks fetch or the API module that sends the request replaces the guard, as before. With this alone, ReportLoadStates' UK report test fails: it sends the local-area breakdown request to the production Public API. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Since #1434 the report page starts a UK report's local-area breakdown alongside its national run. ReportLoadStates' UK report test mocked neither fetchLocalAreaBreakdown nor the hook that calls it, so every run sent GET /uk/economy/98557/over/2 to the production Public API. The test now mocks fetchLocalAreaBreakdown with a request that stays in flight, as it already does for the metadata request, and its UK report uses UK fixture policies (current law 1, reform 98746) in place of the US ones it relabelled as UK. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
A fetch mock that answers the URLs a test lists and refuses every other request as the guard does, so a test can mock the requests it expects without letting any other one through. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- v3CleanSlate: runFlagshipReport starts the report's calculation without waiting for it, and nothing mocked fetchSocietyWideCalculation, so the test sent GET /us/economy/77/over/2 to the production Public API. It now mocks that function with a request that stays in flight. - BillReport: the page asks the calculator's own /api/model-validation, /api/scorecard-comparisons and /api/calibration-targets routes. Their relative URLs could not be sent from the test environment, and the page read the failure as "unavailable". The tests now answer them with 404, as outside calculator-app, and refuse any other request. - AbsoluteChangeByDistrict and RelativeChangeByDistrict: the district map loads /data/geojson/congressional_districts.geojson, which failed the same way. The tests now answer it with the map test's GeoJSON fixture. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
anth-volk
marked this pull request as ready for review
October 30, 2026 16:19
anth-volk
added a commit
that referenced
this pull request
Nov 2, 2026
main's unit tests now refuse network requests nothing mocked (#1504), and its AbsoluteChangeByDistrict and RelativeChangeByDistrict tests answer the district map's GeoJSON request. This branch rewrote both tests around fakeStateDistrictApi, which replaces fetch, so they keep this branch's version, and the fake API now answers the boundary file with the map test's GeoJSON fixture instead of a 404. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 9, 2026
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1503
What this changes
ReportLoadStates.test.tsxno longer reaches production. Its UK report test rendered the report page, which since #1434 starts the UK local-area breakdown alongside the national run. Nothing mocked that request, so every run sentGET /uk/economy/98557/over/2to the production Public API: 213 requests since 27 October, 202 of them from CI. The test now mocksfetchLocalAreaBreakdownwith a request that stays in flight (as it already does for metadata), and its UK report uses UK fixture policies instead of US ones relabelled as UK.App tests can no longer reach the network.
app/vitest.setup.mjsinstalls a guard fromapp/test-utils/unmockedRequestGuard.tsbefore each test file:fetchrejects at once with anUnmockedRequestErrornaming the request's method and URL. In jsdom suites,XMLHttpRequest'ssendthrows the same error (jsdom's XMLHttpRequest makes real requests, and Plotly uses it).vi.stubGlobal('fetch', …),vi.spyOn(globalThis, 'fetch'), assigningglobal.fetch, or mocking the API module.vi.unstubAllGlobals()brings the guard back.fetchRespondingTo({ [url]: () => response }), exported from@test-utils, is a fetch mock that answers the listed URLs and refuses every other request as the guard does.src/tests/unit/test-utils/unmockedRequestGuard.test.tscovers the guard. Its requests go to a reserved.testhost, so even without the guard they could not reach a real server.Other tests the guard found in CI
libs/flagship/v3CleanSlate.test.tsxGET https://api.policyengine.org/us/economy/77/over/2, sent to the production Public API byrunFlagshipReportfetchSocietyWideCalculationwith a request that stays in flightpages/flagship/BillReport.test.tsxPOST /api/model-validation,/api/scorecard-comparisons,/api/calibration-targetspages/report-output/congressional-district/AbsoluteChangeByDistrict.test.tsxandRelativeChangeByDistrict.test.tsxGET /data/geojson/congressional_districts.geojsonThe relative URLs never left the test process (Node's
fetchcannot parse them), but the tests depended on that failure without saying so; they now mock those requests explicitly.Commits
ReportLoadStates.test.tsxwith the unmocked breakdown request.ReportLoadStates.test.tsxfix.fetchRespondingTo, with its test.Tests
Run individually:
bun run vitest -- src/tests/unit/test-utils/unmockedRequestGuard.test.ts src/tests/unit/pages/flagship/ReportLoadStates.test.tsx src/tests/unit/libs/flagship/v3CleanSlate.test.tsx src/tests/unit/pages/flagship/BillReport.test.tsx src/tests/unit/pages/report-output/congressional-district/AbsoluteChangeByDistrict.test.tsx src/tests/unit/pages/report-output/congressional-district/RelativeChangeByDistrict.test.tsx(6 files, 55 tests passed). Type check, ESLint and Prettier pass. CI's Tests job passes: 464 app test files (6,118 passed, 10 skipped) and 58 website test files.🤖 Generated with Claude Code