You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 4, 2026. It is now read-only.
Repository navigation
Commit 19d86fe
Browse filesBrowse the repository at this point in the historyBrowse files
fix(tests): stop test files leaking a replaced global into the shared unit worker (#8538)
The `unit` project runs `isolate: false` — one module graph and one global
object per worker — and `vitest.config.mts` writes out the ComponentRegistry
invariant that pays for it, enforced by
`scripts/__tests__/unit-registry-absence-collision.test.ts`. `globalThis` is the
second invariant of the same class and had no gate:
a global the setup files install must be the same value at the END of a file
as it was at the START of it.
It was already false. Four files replaced `globalThis.fetch` with a bare
`vi.fn()` by assignment, and a fifth left a `vi.stubGlobal('localStorage', ...)`
standing, so the network-escape guard's `guardedFetch` wrapper was gone for
every later file in the worker and `scripts/__tests__/network-escape-ledger.test.ts`
— a file that did nothing wrong — went red on a full `--project unit` run
reading `Mock` where the wrapper belonged.
Both halves:
- the writers hand the global back: `vi.stubGlobal` in, `vi.unstubAllGlobals()`
out. A bare assignment is undone by neither `vi.unstubAllGlobals()` nor
`vi.restoreAllMocks()`, because neither knows the value was ever replaced.
- `vitest.setup.shared-global-leak-guard.ts` arms a per-file `afterAll` in every
`isolate: false` project: it snapshots what the setup files installed, reds the
file that left one replaced, and puts the value back so no innocent file
inherits it. The wiring and the detector are pinned by
`scripts/__tests__/shared-global-leak-guard.test.ts`.
It has to be a setup file: measured, Vitest re-executes `setupFiles` per test
file even under `isolate: false`, while a module those setup files IMPORT is
evaluated once per worker — so a hook registered in such a module attaches to the
first test file of the worker and to no other.
The new guard immediately found a second leak that nothing could previously see:
`unit-registry-absence-collision.test.ts` imports the project's closures into a
private module graph, and one of the specifiers it resolves is
`vitest.setup.network-escape-guard`, so `vi.resetModules()` made it re-execute
and install a SECOND `guardedFetch` over the first. Both wrappers carry the same
name, so nothing downstream could detect the swap. That gate now snapshots and
restores the globals around its measurement.
Claude-Session: https://claude.ai/code/session_01FhBNJcLRZLe8M87VcUgpKr
Co-authored-by: claude[bot] <noreply@anthropic.com>
Test-infrastructure only; no published behaviour changes. The `unit` Vitest project runs `isolate: false`, so its worker shares one `globalThis`, and five test files left a double on a shared global instead of handing it back — which is why `scripts/__tests__/network-escape-ledger.test.ts` reported a `vi.fn()` where the network-escape guard's `fetch` wrapper belonged. Those files now stub and unstub, and a new per-file guard reds the file that leaks a shared global instead of letting the failure surface in a later, innocent one.
0 commit comments