vitest-dev/vitest · error · TypeError
`bench()` expects a benchmark function. Call `bench(name…
Error message
`bench()` expects a benchmark function. Call `bench(name, fn)` or `bench(name, options, fn)`.
What it means
When the second argument to `bench(name, a, b)` is an options object (`a` is not a function), the third argument (`b`) must be the benchmark function. This `TypeError` fires when `b` is missing or is not a function. In other words: `bench(name, options)` without a function, or `bench(name, options, nonFunction)`.
Solutions
- Provide the benchmark function as the third argument: `bench('x', options, fn)`.
- If you have no options, use the two-argument form: `bench('x', fn)`.
Example fix
// before
bench('sort', { iterations: 100 })
// after
bench('sort', { iterations: 100 }, sortFn) Defensive patterns
Strategy: validation
Validate before calling
if (typeof a !== 'function' && typeof b !== 'function') throw new TypeError('missing benchmark function') Type guard
null
Try / catch
null
Prevention
- Always provide the function argument when passing options.
- Prefer `bench(name, fn)` when no options are needed.
- Let TypeScript's overload signatures guide the call.
When it happens
Trigger: Calling `bench('x', { iterations: 100 })` with no third arg; calling `bench('x', opts, 'not-a-fn')`; calling `bench('x', opts, null)`. The guard at benchmark.ts:578-580 fires after the first branch (`typeof a === 'function'`) is false.
Common situations: Forgetting the function when adding options; refactor that drops the function argument; passing a string description instead of a function.
Related errors
- `bench.from()` expects a string path or a function…
- `bench.from()` requires a name (string or named function)…
- `bench.compare()` expects every argument to be the return…
- `bench.compare()` requires at least 2 benchmarks, received
- `bench()` does not accept options as the third argument…
AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11).
Data as JSON: /api/errors/fe7ad0e15e4074e4.
Report an issue: GitHub.
Appendix: source
Thrown at packages/vitest/src/runtime/benchmark.ts:579
function formatModuleId(moduleId: string, root: string): string {
if (!root || !isAbsolute(moduleId)) {
return moduleId
}
return relative(root, moduleId)
}
function normalizeBenchArgs(
a: BenchFn | BenchFnOptions,
b: BenchFn | BenchFnOptions | undefined,
): { fn: BenchFn; fnOpts: BenchOptions | undefined; writeResult: string | undefined; perProject: boolean } {
if (typeof a === 'function') {
if (b !== undefined) {
throw new TypeError('`bench()` does not accept options as the third argument. Pass options as the second argument instead: `bench(name, options, fn)`.')
}
return { fn: a, fnOpts: undefined, writeResult: undefined, perProject: false }
}
if (typeof b !== 'function') {
throw new TypeError('`bench()` expects a benchmark function. Call `bench(name, fn)` or `bench(name, options, fn)`.')
}
// Strip vitest-specific fields only when present so we don't allocate a new
// object — preserving referential identity matters: users inspect
// `registration.fnOpts` and the provider sees the same object the caller
// passed in.
if (a.writeResult === undefined && a.perProject === undefined) {
return { fn: b, fnOpts: a as BenchOptions, writeResult: undefined, perProject: false }
}
const { writeResult, perProject, ...fnOpts } = a
return {
fn: b,
fnOpts: Object.keys(fnOpts).length > 0 ? fnOpts as BenchOptions : undefined,
writeResult,
perProject: perProject ?? false,
}
}
function validateBenchmarkProject(config: SerializedConfig) {View on GitHub (pinned to 1fa9837ec2)