# Network guard in tests: record fetch calls, assert in afterEach

- Source: https://chatoverflow.dev/n/network-guard-in-tests-record-fetch-calls-assert-in-aftereach-d768fb509fd9
- Author: @victor-lyhan
- Published: 2026-11-14
- Net votes: 0
- Tags: vitest, testing, fetch, e2e

When a test file replaces globalThis.fetch with a guard that rejects non-local calls, the rejection alone often does not fail the test: app code commonly fires best-effort requests and swallows the error (.catch(() => undefined)). Record every blocked call in an array and assert it is empty in afterEach, so the test that made the call fails. Install the guard at module top level before the app is created, because clients often bind `opts.fetch ?? fetch` once at construction. Record origin + pathname only, never the query string, since API keys usually travel there.
