Skip to content

Commit 3675fd1

Browse files
committed
Centralize Logger mock in vitest-setup.ts, remove per-file duplicates
Move the Logger vi.mock into a single setupFiles entry so all test files get it automatically. Remove the duplicated mock from 6 test files and add vi.unmock in logger.test.ts which needs the real implementation. https://claude.ai/code/session_01P16MVHHxCnhKCYdTCNayv3
1 parent 91e7c66 commit 3675fd1

10 files changed

+37
-87
lines changed

src/__tests__/cross-file-prepass.test.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { afterEach, beforeEach, describe, it, expect, vi } from "vitest";
1+
import { afterEach, beforeEach, describe, it, expect } from "vitest";
22
import { join, resolve as pathResolve } from "node:path";
33
import { fileURLToPath } from "node:url";
44
import { dirname } from "node:path";
@@ -25,16 +25,6 @@ import {
2525
patchConsumerFile,
2626
} from "../internal/bridge-consumer-patcher.js";
2727

28-
// Suppress codemod logs in tests
29-
vi.mock("../internal/logger.js", () => ({
30-
Logger: {
31-
info: vi.fn(),
32-
warn: vi.fn(),
33-
error: vi.fn(),
34-
logWarnings: vi.fn(),
35-
},
36-
}));
37-
3828
const __dirname = dirname(fileURLToPath(import.meta.url));
3929
const fixturesDir = join(__dirname, "fixtures", "cross-file");
4030
const fixture = (name: string) => join(fixturesDir, name);

src/__tests__/extract-external-interface.test.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,12 @@ import {
99
} from "node:fs";
1010
import { tmpdir } from "node:os";
1111
import path from "node:path";
12-
import { describe, it, expect, beforeAll, afterAll, vi } from "vitest";
12+
import { describe, it, expect, beforeAll, afterAll } from "vitest";
1313

1414
import type { ExternalInterfaceResult } from "../adapter.js";
1515
import { createModuleResolver } from "../internal/prepass/resolve-imports.js";
1616
import { runPrepass } from "../internal/prepass/run-prepass.js";
1717

18-
// Suppress codemod logs in tests
19-
vi.mock("../internal/logger.js", () => ({
20-
Logger: {
21-
info: vi.fn(),
22-
warn: vi.fn(),
23-
error: vi.fn(),
24-
logWarnings: vi.fn(),
25-
},
26-
}));
27-
2818
/** Recursively collect all .tsx/.ts/.jsx files in a directory. */
2919
function collectFiles(dir: string): string[] {
3020
const files: string[] = [];

src/__tests__/forwarded-as-patcher.test.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, it, expect, vi } from "vitest";
1+
import { describe, it, expect } from "vitest";
22
import { join } from "node:path";
33
import { fileURLToPath } from "node:url";
44
import { dirname } from "node:path";
@@ -10,16 +10,6 @@ import {
1010
patchConsumerForwardedAs,
1111
} from "../internal/forwarded-as-consumer-patcher.js";
1212

13-
// Suppress codemod logs in tests
14-
vi.mock("../internal/logger.js", () => ({
15-
Logger: {
16-
info: vi.fn(),
17-
warn: vi.fn(),
18-
error: vi.fn(),
19-
logWarnings: vi.fn(),
20-
},
21-
}));
22-
2313
const __dirname = dirname(fileURLToPath(import.meta.url));
2414
const fixturesDir = join(__dirname, "fixtures", "cross-file");
2515
const fixture = (name: string) => join(fixturesDir, name);

src/__tests__/logger.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { describe, it, expect, beforeEach, vi } from "vitest";
2+
3+
vi.unmock("../internal/logger.js");
24
import { Logger } from "../internal/logger.js";
35

46
describe("Logger", () => {

src/__tests__/run-prepass-rg-prefilter.test.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,6 @@ import { tmpdir } from "node:os";
33
import path from "node:path";
44
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
55

6-
// Suppress codemod logs in tests
7-
vi.mock("../internal/logger.js", () => ({
8-
Logger: {
9-
info: vi.fn(),
10-
warn: vi.fn(),
11-
error: vi.fn(),
12-
logWarnings: vi.fn(),
13-
},
14-
}));
15-
166
interface FixturePaths {
177
root: string;
188
componentFile: string;

src/__tests__/run-prepass-selector-prefilter.test.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
1-
import { describe, expect, it, vi } from "vitest";
1+
import { describe, expect, it } from "vitest";
22
import { dirname, join, resolve as pathResolve } from "node:path";
33
import { fileURLToPath } from "node:url";
44

55
import { createModuleResolver } from "../internal/prepass/resolve-imports.js";
66
import { runPrepass } from "../internal/prepass/run-prepass.js";
7-
8-
// Suppress codemod logs in tests
9-
vi.mock("../internal/logger.js", () => ({
10-
Logger: {
11-
info: vi.fn(),
12-
warn: vi.fn(),
13-
error: vi.fn(),
14-
logWarnings: vi.fn(),
15-
},
16-
}));
177
import {
188
scanCrossFileSelectors,
199
type CrossFileInfo,

src/__tests__/run.e2e.test.ts

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,11 @@
1-
import { describe, it, expect, vi } from "vitest";
1+
import { describe, it, expect } from "vitest";
22
import { mkdtemp, copyFile, mkdir, readFile } from "node:fs/promises";
33
import { tmpdir } from "node:os";
44
import { join, dirname } from "node:path";
55
import { fileURLToPath } from "node:url";
66
import { execFileSync } from "node:child_process";
77
import { format } from "oxfmt";
88
import { runTransform } from "../run.js";
9-
10-
// Suppress codemod logs in tests
11-
vi.mock("../internal/logger.js", () => ({
12-
Logger: {
13-
info: vi.fn(),
14-
warn: vi.fn(),
15-
error: vi.fn(),
16-
logError: vi.fn(),
17-
logWarnings: vi.fn(),
18-
createReport: vi.fn(() => ({
19-
toString: () => "",
20-
print: vi.fn(),
21-
getWarnings: vi.fn(() => []),
22-
})),
23-
setFileCount: vi.fn(),
24-
setMaxExamples: vi.fn(),
25-
markErrorAsLogged: vi.fn(),
26-
isErrorLogged: vi.fn(() => false),
27-
_clearCollected: vi.fn(),
28-
},
29-
}));
309
import {
3110
runTransform as runTransformFromIndex,
3211
defineAdapter as defineAdapterFromIndex,

src/__tests__/transform.test.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, it, expect, vi } from "vitest";
1+
import { describe, it, expect } from "vitest";
22
import { applyTransform } from "jscodeshift/src/testUtils.js";
33
import jscodeshift from "jscodeshift";
44
import { readdirSync, readFileSync, existsSync } from "node:fs";
@@ -23,16 +23,6 @@ function isBailOutFixture(filename: string): boolean {
2323
return BAIL_OUT_PREFIXES.some((prefix) => filename.startsWith(prefix));
2424
}
2525

26-
// Suppress codemod logs in tests
27-
vi.mock("../internal/logger.js", () => ({
28-
Logger: {
29-
info: vi.fn(),
30-
warn: vi.fn(),
31-
error: vi.fn(),
32-
logWarnings: vi.fn(),
33-
},
34-
}));
35-
3626
const __dirname = dirname(fileURLToPath(import.meta.url));
3727

3828
const testCasesDir = join(__dirname, "..", "..", "test-cases");

src/__tests__/vitest-setup.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Vitest global setup: mock Logger to suppress codemod diagnostic output
3+
* during test runs so that the agent reporter output stays clean.
4+
*
5+
* Tests that need the real Logger (e.g. logger.test.ts) should call
6+
* vi.unmock("../internal/logger.js") at the top of the file.
7+
*/
8+
import { vi } from "vitest";
9+
10+
vi.mock("../internal/logger.js", () => ({
11+
Logger: {
12+
info: vi.fn(),
13+
warn: vi.fn(),
14+
error: vi.fn(),
15+
logError: vi.fn(),
16+
logWarnings: vi.fn(),
17+
createReport: vi.fn(() => ({
18+
toString: () => "",
19+
print: vi.fn(),
20+
getWarnings: vi.fn(() => []),
21+
})),
22+
setFileCount: vi.fn(),
23+
setMaxExamples: vi.fn(),
24+
markErrorAsLogged: vi.fn(),
25+
isErrorLogged: vi.fn(() => false),
26+
_clearCollected: vi.fn(),
27+
},
28+
}));

vitest.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export default defineConfig({
88
testTimeout: 20000,
99
hookTimeout: 10000,
1010
reporters: ["agent"],
11+
setupFiles: ["src/__tests__/vitest-setup.ts"],
1112
coverage: {
1213
provider: "v8",
1314
reporter: ["text", "json", "html"],

0 commit comments

Comments
 (0)