Skip to content
Merged
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
6 changes: 4 additions & 2 deletions packages/vitest-plugin/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ export default defineConfig([
{
input: "src/analysis.ts",
output: { file: "dist/analysis.mjs", format: "es" },
plugins: jsPlugins(pkg.version),
// top-level await
plugins: jsPlugins(pkg.version, "es2022"),
external: ["@codspeed/core", /^vitest/],
},
{
input: "src/walltime/index.ts",
output: { file: "dist/walltime.mjs", format: "es" },
plugins: jsPlugins(pkg.version),
// top-level await
plugins: jsPlugins(pkg.version, "es2022"),
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,7 +1,7 @@
import { fromPartial } from "@total-typescript/shoehorn";
import { describe, expect, it, vi, type RunnerTestSuite } from "vitest";
import { getBenchFn } from "vitest/suite";
import { AnalysisRunner as CodSpeedRunner } from "../analysis";
import { getBenchFn } from "../compat";

const coreMocks = vi.hoisted(() => {
return {
Expand All @@ -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
3 changes: 1 addition & 2 deletions packages/vitest-plugin/src/analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import {
wrapWithRootFrame,
} from "@codspeed/core";
import { Benchmark, type RunnerTestSuite } from "vitest";
import { NodeBenchmarkRunner } from "vitest/runners";
import { getBenchFn } from "vitest/suite";
import {
callSuiteHook,
isVitestTaskBenchmark,
patchRootSuiteWithFullFilePath,
} from "./common";
import { getBenchFn, NodeBenchmarkRunner } from "./compat";

const currentFileName =
typeof __filename === "string"
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest-plugin/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getGitDir } from "@codspeed/core";
import path from "path";
import { Benchmark, type RunnerTask, type RunnerTestSuite } from "vitest";
import { getHooks } from "vitest/suite";
import { getHooks } from "./compat";
type SuiteHooks = ReturnType<typeof getHooks>;

function getSuiteHooks(suite: RunnerTestSuite, name: keyof SuiteHooks) {
Expand Down
35 changes: 35 additions & 0 deletions packages/vitest-plugin/src/compat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
type VitestExports = typeof import("vitest");

/**
* Vitest 4.1 moved the benchmark runner and the suite helpers to the main
* `vitest` entry point and deprecated the `vitest/runners` and `vitest/suite`
* subpaths, which warn on import.
*/
async function resolveVitestApi() {
const { BenchmarkRunner, TestRunner }: Partial<VitestExports> =
await import("vitest");

if (BenchmarkRunner && TestRunner) {
return {
NodeBenchmarkRunner: BenchmarkRunner,
getHooks: TestRunner.getSuiteHooks,
getBenchFn: TestRunner.getBenchFn,
getBenchOptions: TestRunner.getBenchOptions,
};
}

const [runners, suite] = await Promise.all([
import("vitest/runners"),
import("vitest/suite"),
]);

return {
NodeBenchmarkRunner: runners.NodeBenchmarkRunner,
getHooks: suite.getHooks,
getBenchFn: suite.getBenchFn,
getBenchOptions: suite.getBenchOptions,
};
}

export const { NodeBenchmarkRunner, getHooks, getBenchFn, getBenchOptions } =
await resolveVitestApi();
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 @@ -12,8 +12,8 @@ import {
RunnerTaskResultPack,
type RunnerTestSuite,
} from "vitest";
import { NodeBenchmarkRunner } from "vitest/runners";
import { patchRootSuiteWithFullFilePath } from "../common";
import { NodeBenchmarkRunner } from "../compat";
import { extractBenchmarkResults } from "./utils";

type Tinybench = typeof tinybench;
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,8 +10,8 @@ import {
type RunnerTestSuite,
type Benchmark as VitestBenchmark,
} from "vitest";
import { getBenchOptions } from "vitest/suite";
import { isVitestTaskBenchmark } from "../common";
import { getBenchOptions } from "../compat";

export async function extractBenchmarkResults(
suite: RunnerTestSuite,
Expand Down
3 changes: 2 additions & 1 deletion rollup.options.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import esbuild from "rollup-plugin-esbuild";
*/
export const declarationsPlugin = (options) => [dts(options)];

export const jsPlugins = (version) => [
export const jsPlugins = (version, target = "es2020") => [
json(),
esbuild({
target,
define: {
__VERSION__: '"' + version + '"',
},
Expand Down
Loading