Skip to content

Commit b4bc06c

Browse files
committed
test(doctor): use restorable spyOn for config mock in context.test
context.test.ts replaced the entire config.ts module via mock.module, which is process-lifetime in Bun and omitted getConfigFile/_setConfigDir. When the doctor folder ran in a single `bun test` process, the polluted mock leaked into doctor.test.ts (which needs the real module), crashing with "Export named 'getConfigFile' not found". Swap the config mock for a spyOn on resolveProfile (the only symbol context.ts imports) and restore it in afterAll, so doctor.test.ts gets the real config.ts back.
1 parent 428b2e9 commit b4bc06c

1 file changed

Lines changed: 9 additions & 13 deletions

File tree

packages/cli-core/src/commands/doctor/context.test.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
import { test, expect, describe, mock, beforeEach, afterEach } from "bun:test";
2-
import {
3-
useCaptureLog,
4-
credentialStoreStubs,
5-
configStubs,
6-
gitStubs,
7-
stubFetch,
8-
} from "../../test/lib/stubs.ts";
1+
import { test, expect, describe, mock, spyOn, beforeEach, afterEach, afterAll } from "bun:test";
2+
import { useCaptureLog, credentialStoreStubs, gitStubs, stubFetch } from "../../test/lib/stubs.ts";
3+
import * as config from "../../lib/config.ts";
94
import type { Application } from "../../lib/plapi.ts";
105

116
const mockGetToken = mock();
@@ -15,12 +10,13 @@ mock.module("../../lib/credential-store.ts", () => ({
1510
getToken: (...args: unknown[]) => mockGetToken(...args),
1611
}));
1712

13+
// spyOn (not mock.module) for config: a spy is restorable, so afterAll hands the
14+
// real module back to doctor.test.ts when both run in one `bun test` process.
1815
const mockResolveProfile = mock();
19-
20-
mock.module("../../lib/config.ts", () => ({
21-
...configStubs,
22-
resolveProfile: (...args: unknown[]) => mockResolveProfile(...args),
23-
}));
16+
const resolveProfileSpy = spyOn(config, "resolveProfile").mockImplementation((...args: unknown[]) =>
17+
mockResolveProfile(...(args as [string])),
18+
);
19+
afterAll(() => resolveProfileSpy.mockRestore());
2420

2521
mock.module("../../lib/git.ts", () => gitStubs);
2622

0 commit comments

Comments
 (0)