Skip to content
Open
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
25 changes: 25 additions & 0 deletions packages/vitest-plugin/src/__tests__/instrumented.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,29 @@ describe("CodSpeedRunner", () => {
"teardown:run:test bench",
]);
});
it("should explain a benchmark function it cannot find", async () => {
// What the runner sees when the benches were registered in another
// vitest copy than the one this plugin imports.
mockedGetBenchFn.mockReturnValue(
undefined as unknown as ReturnType<typeof getBenchFn>,
);

const runner = new CodSpeedRunner(fromPartial({}));
const suite = fromPartial<RunnerTestSuite>({
file: { filepath: __filename },
name: "test suite",
tasks: [
{
type: "test",
mode: "run",
meta: { benchmark: true },
name: "test bench",
},
],
});

await expect(runner.runSuite(suite)).rejects.toThrow(
/no benchmark function found for ".*::test bench"\. The benchmark file and @codspeed\/vitest-plugin most likely resolved different copies of vitest/,
);
});
});
10 changes: 10 additions & 0 deletions packages/vitest-plugin/src/legacy/analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ async function runAnalysisBench(
const uri = `${currentSuiteName}::${benchmark.name}`;
const fn = getBenchFn(benchmark);

// The registry `getBenchFn` reads lives in the vitest instance this plugin
// imports. When the benchmark file registered its benches in another copy
// (pnpm installs one per set of peers, e.g. per vite major), it is empty
// here, and calling it fails with a bare "fn is not a function".
if (typeof fn !== "function") {
throw new Error(
`[CodSpeed] no benchmark function found for "${uri}". The benchmark file and @codspeed/vitest-plugin most likely resolved different copies of vitest, so the plugin cannot see the registered benchmarks. Install @codspeed/vitest-plugin in the package that runs the benchmarks and import it from that package's vitest config, so both resolve the same vitest.`,
);
}

// Constructing a Bench applies tinybench's no-op defaults for the setup and
// teardown hooks and gives them the Task they expect. The bench itself is
// never run: this runner drives the benchmark function directly.
Expand Down