{"record":{"id":"fe7ad0e15e4074e4","repo":"vitest-dev/vitest","slug":"bench-expects-a-benchmark-function-call-benc","errorCode":null,"errorMessage":"`bench()` expects a benchmark function. Call `bench(name, fn)` or `bench(name, options, fn)`.","messagePattern":"`bench\\(\\)` expects a benchmark function\\. Call `bench\\(name, fn\\)` or `bench\\(name, options, fn\\)`\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/runtime/benchmark.ts","lineNumber":579,"sourceCode":"function formatModuleId(moduleId: string, root: string): string {\n  if (!root || !isAbsolute(moduleId)) {\n    return moduleId\n  }\n  return relative(root, moduleId)\n}\n\nfunction normalizeBenchArgs(\n  a: BenchFn | BenchFnOptions,\n  b: BenchFn | BenchFnOptions | undefined,\n): { fn: BenchFn; fnOpts: BenchOptions | undefined; writeResult: string | undefined; perProject: boolean } {\n  if (typeof a === 'function') {\n    if (b !== undefined) {\n      throw new TypeError('`bench()` does not accept options as the third argument. Pass options as the second argument instead: `bench(name, options, fn)`.')\n    }\n    return { fn: a, fnOpts: undefined, writeResult: undefined, perProject: false }\n  }\n  if (typeof b !== 'function') {\n    throw new TypeError('`bench()` expects a benchmark function. Call `bench(name, fn)` or `bench(name, options, fn)`.')\n  }\n  // Strip vitest-specific fields only when present so we don't allocate a new\n  // object — preserving referential identity matters: users inspect\n  // `registration.fnOpts` and the provider sees the same object the caller\n  // passed in.\n  if (a.writeResult === undefined && a.perProject === undefined) {\n    return { fn: b, fnOpts: a as BenchOptions, writeResult: undefined, perProject: false }\n  }\n  const { writeResult, perProject, ...fnOpts } = a\n  return {\n    fn: b,\n    fnOpts: Object.keys(fnOpts).length > 0 ? fnOpts as BenchOptions : undefined,\n    writeResult,\n    perProject: perProject ?? false,\n  }\n}\n\nfunction validateBenchmarkProject(config: SerializedConfig) {","sourceCodeStart":561,"sourceCodeEnd":597,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/runtime/benchmark.ts#L561-L597","documentation":"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)`.","triggerScenarios":"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.","commonSituations":"Forgetting the function when adding options; refactor that drops the function argument; passing a string description instead of a function.","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)`."],"exampleFix":"// before\nbench('sort', { iterations: 100 })\n\n// after\nbench('sort', { iterations: 100 }, sortFn)","handlingStrategy":"validation","validationCode":"if (typeof a !== 'function' && typeof b !== 'function') throw new TypeError('missing benchmark function')","typeGuard":"null","tryCatchPattern":"null","preventionTips":["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."],"tags":["benchmark","api-usage","validation","missing-function"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}