Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions packages/vitest-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@
"devDependencies": {
"@total-typescript/shoehorn": "^0.1.1",
"execa": "^8.0.1",
"rolldown": "1.0.0-rc.11",
"tinybench": "^2.9.0",
"vite": "^8.0.0",
"vitest": "^4.0.18"
"vite": "^7.0.0",
"vitest": "^4.1.1"
}
}
10 changes: 7 additions & 3 deletions packages/vitest-plugin/rollup.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { defineConfig } from "rollup";
import { declarationsPlugin, jsPlugins } from "../../rollup.options";
import {
declarationsPlugin,
jsPlugins,
jsPluginsWithTarget,
} from "../../rollup.options";
import pkg from "./package.json" assert { type: "json" };

export default defineConfig([
Expand All @@ -23,13 +27,13 @@ export default defineConfig([
{
input: "src/analysis.ts",
output: { file: "dist/analysis.mjs", format: "es" },
plugins: jsPlugins(pkg.version),
plugins: jsPluginsWithTarget(pkg.version, "esnext"),
external: ["@codspeed/core", /^vitest/],
},
{
input: "src/walltime/index.ts",
output: { file: "dist/walltime.mjs", format: "es" },
plugins: jsPlugins(pkg.version),
plugins: jsPluginsWithTarget(pkg.version, "esnext"),
external: ["@codspeed/core", /^vitest/],
},
]);
6 changes: 3 additions & 3 deletions packages/vitest-plugin/src/__tests__/instrumented.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fromPartial } from "@total-typescript/shoehorn";
import { describe, expect, it, vi, type RunnerTestSuite } from "vitest";
import { getBenchFn } from "vitest/suite";
import { getBenchFn } from "../compat";
import { AnalysisRunner as CodSpeedRunner } from "../analysis";

const coreMocks = vi.hoisted(() => {
Expand Down Expand Up @@ -28,8 +28,8 @@ vi.mock("@codspeed/core", async (importOriginal) => {

console.log = vi.fn();

vi.mock("vitest/suite", async (importOriginal) => {
const actual = await importOriginal<typeof import("vitest/suite")>();
vi.mock("../compat", async (importOriginal) => {
const actual = await importOriginal<typeof import("../compat")>();
return {
...actual,
getBenchFn: vi.fn(),
Expand Down
5 changes: 1 addition & 4 deletions packages/vitest-plugin/src/analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {
teardownCore,
} from "@codspeed/core";
import { Benchmark, type RunnerTestSuite } from "vitest";
import { NodeBenchmarkRunner } from "vitest/runners";
import { getBenchFn } from "vitest/suite";
import { getBenchFn, NodeBenchmarkRunner } from "./compat";
import {
callSuiteHook,
isVitestTaskBenchmark,
Expand Down Expand Up @@ -39,7 +38,6 @@ async function runAnalysisBench(

await optimizeFunction(async () => {
await callSuiteHook(suite, benchmark, "beforeEach");
// @ts-expect-error we do not need to bind the function to an instance of tinybench's Bench
await fn();
await callSuiteHook(suite, benchmark, "afterEach");
});
Expand All @@ -49,7 +47,6 @@ async function runAnalysisBench(
global.gc?.();
await (async function __codspeed_root_frame__() {
InstrumentHooks.startBenchmark();
// @ts-expect-error we do not need to bind the function to an instance of tinybench's Bench
await fn();
InstrumentHooks.stopBenchmark();
InstrumentHooks.setExecutedBenchmark(process.pid, uri);
Expand Down
6 changes: 4 additions & 2 deletions packages/vitest-plugin/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { getGitDir } from "@codspeed/core";
import path from "path";
import { Benchmark, type RunnerTask, type RunnerTestSuite } from "vitest";
import { getHooks } from "vitest/suite";
type SuiteHooks = ReturnType<typeof getHooks>;
import { getHooks } from "./compat";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type SuiteHooks = Record<string, any[]>;

function getSuiteHooks(suite: RunnerTestSuite, name: keyof SuiteHooks) {
return getHooks(suite)?.[name] ?? [];
Expand Down
50 changes: 50 additions & 0 deletions packages/vitest-plugin/src/compat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Compatibility layer for vitest imports across versions.
*
* Vitest 4.1 deprecated `vitest/runners` and `vitest/suite` subpath imports,
* moving exports to the main `vitest` entry point. This module resolves
* the correct imports at runtime to avoid deprecation warnings while
* maintaining compatibility with vitest 3.2+.
*/

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type AnyClass = new (...args: any[]) => any;

// Resolve NodeBenchmarkRunner: vitest >= 4.1 exports it as BenchmarkRunner
// from the main entry; older versions export it from vitest/runners.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const vitestMod: any = await import("vitest");
const _BenchmarkRunner: AnyClass | undefined = vitestMod.BenchmarkRunner;

let _NodeBenchmarkRunner: AnyClass;
if (_BenchmarkRunner) {
_NodeBenchmarkRunner = _BenchmarkRunner;
} else {
const runners = await import("vitest/runners");
_NodeBenchmarkRunner = runners.NodeBenchmarkRunner;
}
export const NodeBenchmarkRunner = _NodeBenchmarkRunner;

// Resolve suite helpers: vitest >= 4.1 exposes them as TestRunner static
// methods; older versions export them from vitest/suite.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type SuiteFn = (...args: any[]) => any;
let _getHooks: SuiteFn;
let _getBenchFn: SuiteFn;
let _getBenchOptions: SuiteFn;

const TestRunner = vitestMod.TestRunner;
if (TestRunner?.getSuiteHooks) {
_getHooks = TestRunner.getSuiteHooks;
_getBenchFn = TestRunner.getBenchFn;
_getBenchOptions = TestRunner.getBenchOptions;
} else {
const suite = await import("vitest/suite");
_getHooks = suite.getHooks;
_getBenchFn = suite.getBenchFn;
_getBenchOptions = suite.getBenchOptions;
}

export const getHooks = _getHooks;
export const getBenchFn = _getBenchFn;
export const getBenchOptions = _getBenchOptions;
2 changes: 1 addition & 1 deletion packages/vitest-plugin/src/walltime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
RunnerTaskResultPack,
type RunnerTestSuite,
} from "vitest";
import { NodeBenchmarkRunner } from "vitest/runners";
import { NodeBenchmarkRunner } from "../compat";
import { patchRootSuiteWithFullFilePath } from "../common";
import { extractBenchmarkResults } from "./utils";

Expand Down
2 changes: 1 addition & 1 deletion packages/vitest-plugin/src/walltime/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
type RunnerTaskResult,
type RunnerTestSuite,
} from "vitest";
import { getBenchOptions } from "vitest/suite";
import { getBenchOptions } from "../compat";
import { isVitestTaskBenchmark } from "../common";

export async function extractBenchmarkResults(
Expand Down
6 changes: 1 addition & 5 deletions packages/vitest-plugin/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
"compilerOptions": {
"outDir": "dist",
"rootDir": "src",
"baseUrl": ".",
"typeRoots": ["node_modules/@types", "../../node_modules/@types"],
"paths": {
"vite": ["node_modules/vite/dist/node"]
}
"typeRoots": ["node_modules/@types", "../../node_modules/@types"]
},
"references": [{ "path": "../core" }],
"include": ["src/**/*.ts"]
Expand Down
Loading
Loading