Skip to content
This repository was archived by the owner on Nov 4, 2026. It is now read-only.

Commit 19d86fe

Browse files
claude[bot]claude
andauthored
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>
1 parent 96cf3d2 commit 19d86fe

11 files changed

Lines changed: 588 additions & 21 deletions

‎.changeset/tall-doors-smash.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
---
3+
4+
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.

‎packages/app-shell/src/preview/__tests__/commitHistory.test.ts‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ import {
3333

3434
afterEach(() => {
3535
vi.restoreAllMocks();
36+
// `vi.restoreAllMocks()` does NOT undo a global replaced by hand, and this
37+
// file runs in the `unit` project, which is `isolate: false` — a `fetch` left
38+
// behind here is inherited by every later file in the worker (objectui#8500).
39+
// So the doubles below go in through `vi.stubGlobal` and come back out here.
40+
vi.unstubAllGlobals();
3641
});
3742

3843
/** A response whose body is JSON. */
@@ -55,7 +60,7 @@ function unparseableResponse(status: number) {
5560
}
5661

5762
function mockFetch(response: unknown) {
58-
global.fetch = vi.fn(async () => response) as unknown as typeof fetch;
63+
vi.stubGlobal('fetch', vi.fn(async () => response) as unknown as typeof fetch);
5964
}
6065

6166
/**
@@ -172,9 +177,12 @@ describe('fetchCommits — fail-loud, on every failing path', () => {
172177
[
173178
'network rejection',
174179
() => {
175-
global.fetch = vi.fn(async () => {
176-
throw new TypeError('Failed to fetch');
177-
}) as unknown as typeof fetch;
180+
vi.stubGlobal(
181+
'fetch',
182+
vi.fn(async () => {
183+
throw new TypeError('Failed to fetch');
184+
}) as unknown as typeof fetch,
185+
);
178186
},
179187
],
180188
])('%s never resolves, and never resolves to an empty history', async (_label, arrange) => {
@@ -189,9 +197,12 @@ describe('fetchCommits — fail-loud, on every failing path', () => {
189197
});
190198

191199
it('a rejected fetch is NOT dressed up as retryable', async () => {
192-
global.fetch = vi.fn(async () => {
193-
throw new TypeError('Failed to fetch');
194-
}) as unknown as typeof fetch;
200+
vi.stubGlobal(
201+
'fetch',
202+
vi.fn(async () => {
203+
throw new TypeError('Failed to fetch');
204+
}) as unknown as typeof fetch,
205+
);
195206

196207
const err = await fetchCommits('com.x').catch((e: unknown) => e);
197208

‎packages/core/src/actions/__tests__/ActionRunner.bodyExtra.test.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* stash would fire on nearly every declared-action click.
2828
*/
2929

30-
import { describe, it, expect, vi, beforeEach, afterEach, type MockInstance } from 'vitest';
30+
import { afterAll, describe, it, expect, vi, beforeEach, afterEach, type MockInstance } from 'vitest';
3131
import { ActionRunner } from '../ActionRunner';
3232
import { resetActionKeyWarnings } from '../actionKeys';
3333

@@ -38,9 +38,17 @@ function sentBody(): any {
3838
}
3939

4040
function okFetch() {
41-
global.fetch = vi.fn().mockResolvedValue({ ok: true, json: vi.fn().mockResolvedValue({}) });
41+
// `vi.stubGlobal` rather than `global.fetch = ...`: this file runs in the
42+
// `unit` project, which is `isolate: false`, so a raw assignment to a shared
43+
// global outlives the file and every later file in the worker inherits it
44+
// (objectui#8500). Only a stub can be handed back.
45+
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: true, json: vi.fn().mockResolvedValue({}) }));
4246
}
4347

48+
afterAll(() => {
49+
vi.unstubAllGlobals();
50+
});
51+
4452
describe('ActionRunner.executeAPI — bodyExtra', () => {
4553
beforeEach(() => {
4654
resetActionKeyWarnings();

‎packages/core/src/actions/__tests__/ActionRunner.bodyShape.test.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* ordering, so there was one reading to mirror rather than a choice to make.
2727
*/
2828

29-
import { describe, it, expect, vi, beforeEach } from 'vitest';
29+
import { describe, it, expect, vi, afterAll, beforeEach } from 'vitest';
3030
import { ActionRunner } from '../ActionRunner';
3131
import { resetActionKeyWarnings } from '../actionKeys';
3232

@@ -37,9 +37,17 @@ function sentBody(): any {
3737
}
3838

3939
function okFetch() {
40-
global.fetch = vi.fn().mockResolvedValue({ ok: true, json: vi.fn().mockResolvedValue({}) });
40+
// `vi.stubGlobal` rather than `global.fetch = ...`: this file runs in the
41+
// `unit` project, which is `isolate: false`, so a raw assignment to a shared
42+
// global outlives the file and every later file in the worker inherits it
43+
// (objectui#8500). Only a stub can be handed back.
44+
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: true, json: vi.fn().mockResolvedValue({}) }));
4145
}
4246

47+
afterAll(() => {
48+
vi.unstubAllGlobals();
49+
});
50+
4351
describe('ActionRunner.executeAPI — bodyShape', () => {
4452
beforeEach(() => {
4553
resetActionKeyWarnings();

‎packages/core/src/actions/__tests__/ActionRunner.test.ts‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -508,9 +508,17 @@ describe('ActionRunner', () => {
508508
// ==========================================================================
509509

510510
describe('api action type', () => {
511+
// `vi.stubGlobal` rather than `global.fetch = ...`, and handed back here:
512+
// this file runs in the `unit` project, which is `isolate: false`, so a raw
513+
// assignment to a shared global outlives the file and every later file in
514+
// the worker inherits it (objectui#8500).
515+
afterEach(() => {
516+
vi.unstubAllGlobals();
517+
});
518+
511519
it('should call fetch with simple string endpoint', async () => {
512520
const mockResponse = { ok: true, json: vi.fn().mockResolvedValue({ id: 1 }) };
513-
global.fetch = vi.fn().mockResolvedValue(mockResponse);
521+
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(mockResponse));
514522

515523
const result = await runner.execute({
516524
type: 'api',
@@ -527,7 +535,7 @@ describe('ActionRunner', () => {
527535

528536
it('should use endpoint field as alias', async () => {
529537
const mockResponse = { ok: true, json: vi.fn().mockResolvedValue({ ok: true }) };
530-
global.fetch = vi.fn().mockResolvedValue(mockResponse);
538+
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(mockResponse));
531539

532540
const result = await runner.execute({
533541
type: 'api',
@@ -540,7 +548,7 @@ describe('ActionRunner', () => {
540548

541549
it('should support complex API config', async () => {
542550
const mockResponse = { ok: true, json: vi.fn().mockResolvedValue({ done: true }) };
543-
global.fetch = vi.fn().mockResolvedValue(mockResponse);
551+
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(mockResponse));
544552

545553
const result = await runner.execute({
546554
type: 'api',
@@ -565,11 +573,11 @@ describe('ActionRunner', () => {
565573
});
566574

567575
it('should handle HTTP errors', async () => {
568-
global.fetch = vi.fn().mockResolvedValue({
576+
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
569577
ok: false,
570578
status: 404,
571579
statusText: 'Not Found',
572-
});
580+
}));
573581

574582
const result = await runner.execute({
575583
type: 'api',
@@ -581,7 +589,7 @@ describe('ActionRunner', () => {
581589
});
582590

583591
it('should handle network errors', async () => {
584-
global.fetch = vi.fn().mockRejectedValue(new Error('Network error'));
592+
vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new Error('Network error')));
585593

586594
const result = await runner.execute({
587595
type: 'api',

‎packages/core/src/actions/__tests__/UndoManager.test.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
import { describe, it, expect, beforeEach, vi } from 'vitest';
9+
import { afterEach, describe, it, expect, beforeEach, vi } from 'vitest';
1010
import { UndoManager, type UndoableOperation } from '../UndoManager';
1111

1212
function makeOp(id: string, type: 'create' | 'update' | 'delete' = 'update'): UndoableOperation {
@@ -234,6 +234,15 @@ describe('UndoManager', () => {
234234
});
235235
});
236236

237+
// Handed back, not left standing: this file runs in the `unit` project,
238+
// which is `isolate: false`, so `vi`'s stub registry is shared across FILES
239+
// in the worker and a stub left here is undone by whichever later file calls
240+
// `vi.unstubAllGlobals()` first — restoring a value that file never
241+
// installed (objectui#8500).
242+
afterEach(() => {
243+
vi.unstubAllGlobals();
244+
});
245+
237246
it('round-trips undo/redo stacks through localStorage', () => {
238247
manager.push(makeOp('1'));
239248
manager.push(makeOp('2'));

0 commit comments

Comments
 (0)