Skip to content

Commit e074ae9

Browse files
committed
feat!(vitest-plugin): support vitest v3 and drop older versions
1 parent 7b5b6c7 commit e074ae9

File tree

11 files changed

+909
-553
lines changed

11 files changed

+909
-553
lines changed

examples/with-typescript-esm/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
"esbuild-register": "^3.4.2",
1717
"tinybench": "^4.0.1",
1818
"typescript": "^5.1.3",
19-
"vitest": "^1.2.2"
19+
"vitest": "^3.2.4"
2020
}
2121
}

packages/tinybench-plugin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"@types/stack-trace": "^0.0.30",
2929
"esbuild-register": "^3.4.2",
3030
"tinybench": "^4.0.1",
31-
"vitest": "^1.2.2"
31+
"vitest": "^3.2.4"
3232
},
3333
"dependencies": {
3434
"@codspeed/core": "workspace:^4.0.1",

packages/vitest-plugin/benches/hooks.bench.ts

Lines changed: 9 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -8,85 +8,30 @@ import {
88
expect,
99
} from "vitest";
1010

11-
let count = -1;
12-
13-
beforeAll(() => {
14-
count += 1;
15-
});
16-
17-
beforeEach(() => {
18-
count += 1;
19-
});
20-
21-
// the count is multiplied by 2 because the bench function is called twice with codspeed (once for the optimization and once for the actual measurement)
22-
bench("one", () => {
23-
expect(count).toBe(1 * 2);
24-
});
25-
26-
describe("level1", () => {
27-
bench("two", () => {
28-
expect(count).toBe(2 * 2);
29-
});
30-
31-
bench("three", () => {
32-
expect(count).toBe(3 * 2);
33-
});
34-
35-
describe("level 2", () => {
36-
beforeEach(() => {
37-
count += 1;
38-
});
39-
40-
bench("five", () => {
41-
expect(count).toBe(5 * 2);
42-
});
43-
44-
describe("level 3", () => {
45-
bench("seven", () => {
46-
expect(count).toBe(7 * 2);
47-
});
48-
});
49-
});
50-
51-
describe("level 2 bench nested beforeAll", () => {
52-
beforeAll(() => {
53-
count = 0;
54-
});
55-
56-
bench("one", () => {
57-
expect(count).toBe(1 * 2);
58-
});
59-
});
60-
61-
bench("two", () => {
62-
expect(count).toBe(2 * 2);
63-
});
64-
});
65-
66-
describe("hooks cleanup", () => {
67-
let cleanUpCount = 0;
11+
describe("hooks", () => {
12+
let count = 0;
6813
describe("run", () => {
6914
beforeAll(() => {
70-
cleanUpCount += 10;
15+
count += 10;
7116
});
7217
beforeEach(() => {
73-
cleanUpCount += 1;
18+
count += 1;
7419
});
7520
afterEach(() => {
76-
cleanUpCount -= 1;
21+
count -= 1;
7722
});
7823
afterAll(() => {
79-
cleanUpCount -= 10;
24+
count -= 10;
8025
});
8126

8227
bench("one", () => {
83-
expect(cleanUpCount).toBe(11);
28+
expect(count).toBe(11);
8429
});
8530
bench("two", () => {
86-
expect(cleanUpCount).toBe(11);
31+
expect(count).toBe(11);
8732
});
8833
});
8934
bench("end", () => {
90-
expect(cleanUpCount).toBe(0);
35+
expect(count).toBe(0);
9136
});
9237
});

packages/vitest-plugin/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131
"@codspeed/core": "workspace:^4.0.1"
3232
},
3333
"peerDependencies": {
34-
"vite": "^4.2.0 || ^5.0.0 || ^6.0.0",
35-
"vitest": ">=1.2.2"
34+
"vite": "^4.2.0 || ^5.0.0 || ^6.0.0 || ^7.0.0",
35+
"vitest": ">=3.2"
3636
},
3737
"devDependencies": {
3838
"@total-typescript/shoehorn": "^0.1.1",
3939
"execa": "^8.0.1",
40-
"vite": "^5.0.0",
41-
"vitest": "^1.2.2"
40+
"vite": "^7.0.6",
41+
"vitest": "^3.2.4"
4242
}
4343
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ describe("codSpeedPlugin", () => {
8686
if (typeof config !== "function")
8787
throw new Error("config is not a function");
8888

89+
// @ts-expect-error we cannot type the this of the config function
8990
expect(config({}, fromPartial({}))).toStrictEqual({
9091
test: {
9192
globalSetup: [

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { fromPartial } from "@total-typescript/shoehorn";
2-
import { describe, expect, it, Suite, vi } from "vitest";
2+
import { describe, expect, it, vi, type RunnerTestSuite } from "vitest";
33
import { getBenchFn } from "vitest/suite";
44
import { InstrumentedRunner as CodSpeedRunner } from "../instrumented";
55

@@ -37,17 +37,23 @@ vi.mock("vitest/suite", async (importOriginal) => {
3737
const mockedGetBenchFn = vi.mocked(getBenchFn);
3838

3939
describe("CodSpeedRunner", () => {
40-
it("should run the bench functions only twice", async () => {
40+
it("should run the bench function", async () => {
4141
const benchFn = vi.fn();
4242
mockedGetBenchFn.mockReturnValue(benchFn);
4343

4444
const runner = new CodSpeedRunner(fromPartial({}));
45-
const suite = fromPartial<Suite>({
46-
filepath: __filename,
45+
const suite = fromPartial<RunnerTestSuite>({
46+
file: { filepath: __filename },
4747
name: "test suite",
48-
tasks: [{ mode: "run", meta: { benchmark: true }, name: "test bench" }],
48+
tasks: [
49+
{
50+
type: "test",
51+
mode: "run",
52+
meta: { benchmark: true },
53+
name: "test bench",
54+
},
55+
],
4956
});
50-
suite.tasks[0].suite = suite;
5157
await runner.runSuite(suite);
5258

5359
// setup
@@ -80,8 +86,8 @@ describe("CodSpeedRunner", () => {
8086
mockedGetBenchFn.mockReturnValue(benchFn);
8187

8288
const runner = new CodSpeedRunner(fromPartial({}));
83-
const rootSuite = fromPartial<Suite>({
84-
filepath: __filename,
89+
const rootSuite = fromPartial<RunnerTestSuite>({
90+
file: { filepath: __filename },
8591
name: "test suite",
8692
tasks: [
8793
{
@@ -90,6 +96,7 @@ describe("CodSpeedRunner", () => {
9096
mode: "run",
9197
tasks: [
9298
{
99+
type: "test",
93100
mode: "run",
94101
meta: { benchmark: true },
95102
name: "test bench",
@@ -98,9 +105,6 @@ describe("CodSpeedRunner", () => {
98105
},
99106
],
100107
});
101-
rootSuite.tasks[0].suite = rootSuite;
102-
// @ts-expect-error type is not narrow enough, but it is fine
103-
rootSuite.tasks[0].tasks[0].suite = rootSuite.tasks[0];
104108

105109
await runner.runSuite(rootSuite);
106110

packages/vitest-plugin/src/common.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import { getGitDir } from "@codspeed/core";
22
import path from "path";
3-
import { Suite, Task } from "vitest";
3+
import { Benchmark, type RunnerTask, type RunnerTestSuite } from "vitest";
44
import { getHooks } from "vitest/suite";
5-
65
type SuiteHooks = ReturnType<typeof getHooks>;
76

8-
function getSuiteHooks(suite: Suite, name: keyof SuiteHooks) {
7+
function getSuiteHooks(suite: RunnerTestSuite, name: keyof SuiteHooks) {
98
return getHooks(suite)?.[name] ?? [];
109
}
1110

1211
export async function callSuiteHook<T extends keyof SuiteHooks>(
13-
suite: Suite,
14-
currentTask: Task,
12+
suite: RunnerTestSuite,
13+
currentTask: RunnerTask,
1514
name: T
1615
): Promise<void> {
1716
if (name === "beforeEach" && suite?.suite) {
@@ -20,20 +19,22 @@ export async function callSuiteHook<T extends keyof SuiteHooks>(
2019

2120
const hooks = getSuiteHooks(suite, name);
2221

22+
// @ts-expect-error TODO: add support for hooks parameters
2323
await Promise.all(hooks.map((fn) => fn()));
2424

2525
if (name === "afterEach" && suite?.suite) {
2626
await callSuiteHook(suite.suite, currentTask, name);
2727
}
2828
}
2929

30-
export function patchRootSuiteWithFullFilePath(suite: Suite) {
31-
if (suite.filepath === undefined) {
32-
throw new Error("filepath is undefined is the root suite");
33-
}
34-
const gitDir = getGitDir(suite.filepath);
30+
export function patchRootSuiteWithFullFilePath(suite: RunnerTestSuite) {
31+
const gitDir = getGitDir(suite.file.filepath);
3532
if (gitDir === undefined) {
3633
throw new Error("Could not find a git repository");
3734
}
38-
suite.name = path.relative(gitDir, suite.filepath);
35+
suite.name = path.relative(gitDir, suite.file.filepath);
36+
}
37+
38+
export function isVitestTaskBenchmark(task: RunnerTask): task is Benchmark {
39+
return task.type === "test" && task.meta.benchmark === true;
3940
}

packages/vitest-plugin/src/index.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from "@codspeed/core";
99
import { join } from "path";
1010
import { Plugin } from "vite";
11-
import { UserConfig } from "vitest/config";
11+
import { type ViteUserConfig } from "vitest/config";
1212

1313
// get this file's directory path from import.meta.url
1414
const __dirname = new URL(".", import.meta.url).pathname;
@@ -45,9 +45,11 @@ export default function codspeedPlugin(): Plugin {
4545
return true;
4646
},
4747
enforce: "post",
48-
config(): UserConfig {
48+
config(): ViteUserConfig {
4949
const runnerFile = getRunnerFile();
50-
const config: UserConfig = {
50+
const runnerMode = getCodspeedRunnerMode();
51+
52+
const config: ViteUserConfig = {
5153
test: {
5254
pool: "forks",
5355
poolOptions: {
@@ -56,14 +58,17 @@ export default function codspeedPlugin(): Plugin {
5658
},
5759
},
5860
globalSetup: [getCodSpeedFileFromName("globalSetup")],
61+
...(runnerFile && {
62+
runner: runnerFile,
63+
}),
64+
...(runnerMode === "walltime" && {
65+
benchmark: {
66+
includeSamples: true,
67+
},
68+
}),
5969
},
6070
};
6171

62-
// Only set custom runner when CODSPEED_ENV is set
63-
if (runnerFile) {
64-
config.test!.runner = runnerFile;
65-
}
66-
6772
return config;
6873
},
6974
};

0 commit comments

Comments
 (0)