Skip to content

Commit 4613be9

Browse files
committed
feat(vitest-plugin): retrieve vitest major version to know which config to apply
1 parent 866b5e1 commit 4613be9

2 files changed

Lines changed: 95 additions & 10 deletions

File tree

packages/vitest-plugin/src/__tests__/index.test.ts

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ const coreMocks = vi.hoisted(() => {
1010
};
1111
});
1212

13+
const fsMocks = vi.hoisted(() => {
14+
let mockVersion = "4.0.18"; // default to v4
15+
return {
16+
readFileSync: vi.fn((path: string) => {
17+
if (path.includes("vitest/package.json")) {
18+
return JSON.stringify({ version: mockVersion });
19+
}
20+
throw new Error(`File not found: ${path}`);
21+
}),
22+
setMockVersion: (version: string) => {
23+
mockVersion = version;
24+
},
25+
};
26+
});
27+
1328
const resolvedCodSpeedPlugin = codspeedPlugin();
1429
const applyPluginFunction = resolvedCodSpeedPlugin.apply;
1530
if (typeof applyPluginFunction !== "function")
@@ -20,6 +35,12 @@ vi.mock("@codspeed/core", async (importOriginal) => {
2035
return { ...mod, ...coreMocks };
2136
});
2237

38+
vi.mock("fs", () => {
39+
return {
40+
readFileSync: fsMocks.readFileSync,
41+
};
42+
});
43+
2344
console.warn = vi.fn();
2445

2546
describe("codSpeedPlugin", () => {
@@ -79,12 +100,51 @@ describe("codSpeedPlugin", () => {
79100
});
80101
});
81102

82-
it("should apply the codspeed config", async () => {
103+
it("should apply the codspeed config for v4", () => {
83104
const config = resolvedCodSpeedPlugin.config;
84105
if (typeof config !== "function")
85106
throw new Error("config is not a function");
86107

87-
expect(config.call({} as never, {}, fromPartial({}))).toStrictEqual({
108+
const result = config.call({} as never, {}, fromPartial({}));
109+
110+
expect(result).toStrictEqual({
111+
test: {
112+
globalSetup: [
113+
expect.stringContaining("packages/vitest-plugin/src/globalSetup.ts"),
114+
],
115+
pool: "forks",
116+
execArgv: [
117+
"--interpreted-frames-native-stack",
118+
"--allow-natives-syntax",
119+
"--hash-seed=1",
120+
"--random-seed=1",
121+
"--no-opt",
122+
"--predictable",
123+
"--predictable-gc-schedule",
124+
"--expose-gc",
125+
"--no-concurrent-sweeping",
126+
"--max-old-space-size=4096",
127+
],
128+
runner: expect.stringContaining(
129+
"packages/vitest-plugin/src/simulation.ts"
130+
),
131+
},
132+
});
133+
});
134+
135+
it("should apply the codspeed config for v3 with poolOptions", () => {
136+
// Set mock version to v3
137+
fsMocks.setMockVersion("3.2.0");
138+
139+
// Create a new plugin instance to pick up the mocked version
140+
const v3Plugin = codspeedPlugin();
141+
const config = v3Plugin.config;
142+
if (typeof config !== "function")
143+
throw new Error("config is not a function");
144+
145+
const result = config.call({} as never, {}, fromPartial({}));
146+
147+
expect(result).toStrictEqual({
88148
test: {
89149
globalSetup: [
90150
expect.stringContaining("packages/vitest-plugin/src/globalSetup.ts"),
@@ -111,5 +171,8 @@ describe("codSpeedPlugin", () => {
111171
),
112172
},
113173
});
174+
175+
// Reset mock version back to v4
176+
fsMocks.setMockVersion("4.0.18");
114177
});
115178
});

packages/vitest-plugin/src/index.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
SetupInstrumentsRequestBody,
77
SetupInstrumentsResponse,
88
} from "@codspeed/core";
9+
import { readFileSync } from "fs";
10+
import { createRequire } from "module";
911
import { join } from "path";
1012
import { Plugin } from "vite";
1113
import { type ViteUserConfig } from "vitest/config";
@@ -20,6 +22,19 @@ function getCodSpeedFileFromName(name: string) {
2022
return join(__dirname, `${name}.${fileExtension}`);
2123
}
2224

25+
function getVitestMajorVersion(): number | null {
26+
try {
27+
// Resolve vitest from the project's perspective (cwd), not from the plugin's location
28+
// This ensures we detect the vitest version the user has installed
29+
const require = createRequire(join(process.cwd(), "package.json"));
30+
const vitestPkgPath = require.resolve("vitest/package.json");
31+
const vitestPkg = JSON.parse(readFileSync(vitestPkgPath, "utf-8"));
32+
return parseInt(vitestPkg.version.split(".")[0], 10);
33+
} catch {
34+
return null;
35+
}
36+
}
37+
2338
function getRunnerFile(): string | undefined {
2439
const codspeedRunnerMode = getCodspeedRunnerMode();
2540
if (codspeedRunnerMode === "disabled") {
@@ -49,18 +64,25 @@ export default function codspeedPlugin(): Plugin {
4964
const runnerFile = getRunnerFile();
5065
const runnerMode = getCodspeedRunnerMode();
5166
const v8Flags = getV8Flags();
67+
const vitestMajorVersion = getVitestMajorVersion();
68+
// by default, assume Vitest v4 or higher
69+
const isVitestV4OrHigher = (vitestMajorVersion ?? 4) >= 4;
5270

5371
const config: ViteUserConfig = {
5472
test: {
5573
pool: "forks",
56-
execArgv: v8Flags,
57-
// @ts-expect-error Compat with Vitest v3
58-
// See: https://vitest.dev/guide/migration.html#pool-rework
59-
poolOptions: {
60-
forks: {
61-
execArgv: v8Flags,
62-
},
63-
},
74+
...(isVitestV4OrHigher
75+
? { execArgv: v8Flags }
76+
: {
77+
// Compat with Vitest v3
78+
// See: https://vitest.dev/guide/migration.html#pool-rework
79+
// poolOptions only exists in Vitest v3
80+
poolOptions: {
81+
forks: {
82+
execArgv: v8Flags,
83+
},
84+
},
85+
}),
6486
globalSetup: [getCodSpeedFileFromName("globalSetup")],
6587
...(runnerFile && {
6688
runner: runnerFile,

0 commit comments

Comments
 (0)