Skip to content

Commit b36a312

Browse files
roodboiclaude
andcommitted
test: make suite order-independent via scoped module mocks
File-scope mock.module poisons every file in a bun test run (module scopes all evaluate before any test executes). Replaced all 10 uses with a scoped dispatching mock helper (activate/deactivate per file), fixed isTTY stub restoration leaks, and reset the gum path cache between tests. Full suite: 642 pass/41 fail -> 683 pass/0 fail, stable under shuffled file order. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent edf7de9 commit b36a312

12 files changed

Lines changed: 789 additions & 387 deletions

tests/env-exec-command.test.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { afterAll, afterEach, expect, mock, test } from "bun:test";
1+
import { afterAll, afterEach, beforeAll, expect, test } from "bun:test";
22
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
33
import { tmpdir } from "node:os";
44
import { join, resolve } from "node:path";
@@ -10,6 +10,7 @@ import {
1010
PROJECT_CONFIG_FILENAME,
1111
} from "../src/constants.ts";
1212
import { setProjectEnvValue } from "../src/lib/project-env-config.ts";
13+
import { registerScopedModuleMock } from "./helpers/scoped-module-mock.ts";
1314

1415
const runCalls: Array<{
1516
readonly cmd: readonly string[];
@@ -20,25 +21,33 @@ const tempDirs = new Set<string>();
2021
const originalHome = process.env.HOME;
2122
const originalShell = process.env.SHELL;
2223

23-
mock.module("../src/lib/shell.ts", () => ({
24-
run: async (
25-
cmd: readonly string[],
26-
opts: {
27-
readonly cwd?: string;
28-
readonly env?: Record<string, string>;
29-
} = {}
30-
) => {
31-
runCalls.push({
32-
cmd: [...cmd],
33-
cwd: opts.cwd,
34-
env: opts.env ? { ...opts.env } : undefined,
35-
});
36-
return 0;
24+
const shellMock = await registerScopedModuleMock({
25+
importerPath: import.meta.path,
26+
specifier: "../src/lib/shell.ts",
27+
overrides: {
28+
run: async (
29+
cmd: readonly string[],
30+
opts: {
31+
readonly cwd?: string;
32+
readonly env?: Record<string, string>;
33+
} = {}
34+
) => {
35+
runCalls.push({
36+
cmd: [...cmd],
37+
cwd: opts.cwd,
38+
env: opts.env ? { ...opts.env } : undefined,
39+
});
40+
return 0;
41+
},
3742
},
38-
}));
43+
});
3944

4045
const { envCommand, hostCommand } = await import("../src/commands/env.ts");
4146

47+
beforeAll(() => {
48+
shellMock.activate();
49+
});
50+
4251
afterEach(async () => {
4352
runCalls.length = 0;
4453
for (const tempDir of tempDirs) {
@@ -50,7 +59,7 @@ afterEach(async () => {
5059
});
5160

5261
afterAll(() => {
53-
mock.restore();
62+
shellMock.deactivate();
5463
});
5564

5665
test("env exec injects merged overlay env into one-off host commands", async () => {

0 commit comments

Comments
 (0)