repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

fix(web): restore globalThis.fetch after every test to stop cross-file leak

47bc8d39a0c1bdd19ffcc37154c3ade2da87bb3f

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

The `web safety/store/map primitives` describe block stubs `globalThis.fetch`
(ending on a `"x".repeat(20)` / text-plain Response) but had no `afterEach` to
restore it — only the sibling `web_fetch context contract` block did. Under
vitest's `isolate: false` (set in 971b99f) that leaked stub bled into later test
files sharing the worker: diff-review's `server-html-full` (10 failures —
`text/plain` ≠ `text/html`, bodies of `x`) and super's `mockup` (2 failures).
Deterministic-red on CI, flaky-green locally (worker-order dependent).

Hoist fetch restoration to a file-level `afterEach`, so every test in every
block hands the global back clean regardless of which block (or future block)
mutated it.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

Changed files (1)

extensions/web/__tests__/web-fetch-contract.test.ts

Modified
31 31 const originalFetch = globalThis.fetch;
33 +// Restore `fetch` after EVERY test in this file. Several tests below swap
34 +// `globalThis.fetch` for a stub, and they live in two sibling `describe`
35 +// blocks — a describe-scoped hook only covers its own block, so the last stub
36 +// set in the second block would otherwise never be restored. Under vitest's
37 +// `isolate: false` that leaked stub bleeds into later test files (e.g.
38 +// diff-review's SSR server tests), which then see the stub's `text/plain` body
39 +// instead of real responses. A file-level hook guarantees the global is always
40 +// handed back clean, regardless of which block (or future block) mutated it.
41 +afterEach(() => {
42 + globalThis.fetch = originalFetch;
43 +});
33 45 async function makeFakeCommand(
34 46 dir: string,
35 47 name: string,
83 95 afterEach(() => {
84 - globalThis.fetch = originalFetch;
85 96 t?.dispose();
86 97 t = undefined;