{"id":"bac948dff8b41bf2","repo":"vitest-dev/vitest","slug":"bench-does-not-accept-options-as-the-third-arg","errorCode":null,"errorMessage":"`bench()` does not accept options as the third argument. Pass options as the second argument instead: `bench(name, options, fn)`.","messagePattern":"`bench\\(\\)` does not accept options as the third argument\\. Pass options as the second argument instead: `bench\\(name, options, fn\\)`\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/runtime/benchmark.ts","lineNumber":574,"sourceCode":"  }\n\n  return bench\n}\n\nfunction 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,","sourceCodeStart":556,"sourceCodeEnd":592,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/src/runtime/benchmark.ts#L556-L592","documentation":"The `bench()` overloads are `bench(name, fn)` and `bench(name, options, fn)` — options come SECOND. `normalizeBenchArgs` (see `benchmark.ts:574`) treats a function second argument as `fn`; if a third argument is also present, it assumes you tried `bench(name, fn, options)` (options last) and throws this `TypeError` to redirect you to the correct order.","triggerScenarios":"Calling `bench('sort', () => x(), { iterations: 100 })` — fn in slot 2, options in slot 3.","commonSituations":"Migrating from another benchmark API (tinybench, jest) where options come last; muscle memory from `it(name, fn, timeout)`.","solutions":["Move options to the second slot: `bench('sort', { iterations: 100 }, () => x())`.","If you don't need options, drop the third argument: `bench('sort', () => x())`."],"exampleFix":"// before\nbench('sort', () => sortArr(), { iterations: 100 })\n\n// after\nbench('sort', { iterations: 100 }, () => sortArr())","handlingStrategy":"validation","validationCode":"// Enforce the correct argument order before calling bench.\nfunction benchSafe(name: string, fnOrOpts: any, maybeOpts?: any) {\n  if (typeof fnOrOpts === 'function' && maybeOpts !== undefined) {\n    // user did bench(name, fn, opts) — reorder\n    return bench(name, maybeOpts, fnOrOpts)\n  }\n  return bench(name, fnOrOpts, maybeOpts)\n}","typeGuard":null,"tryCatchPattern":"try {\n  bench(name, fn, opts)\n} catch (e) {\n  if (e instanceof TypeError && /does not accept options as the third argument/.test(e.message)) {\n    bench(name, opts, fn) // correct order\n  } else throw e\n}","preventionTips":["Remember the signature: bench(name, options?, fn) — options come BEFORE fn.","Drop the options object entirely when you don't need it.","Use an editor snippet that emits the correct argument order."],"tags":["benchmark","bench","api-misuse","argument-order","typeerror"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}