{"id":"1b1faa152f3b429c","repo":"vitest-dev/vitest","slug":"bench-from-requires-a-name-string-or-named-fu","errorCode":null,"errorMessage":"`bench.from()` requires a name (string or named function) as its first argument.","messagePattern":"`bench\\.from\\(\\)` requires a name \\(string or named function\\) as its first argument\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/runtime/benchmark.ts","lineNumber":437,"sourceCode":"      run: (options?: BenchRunOptions) => {\n        pending.delete(registration)\n        return runSingle(name, fn, fnOpts, options, meta, writeResult)\n      },\n    }\n    if (perProject) {\n      registration[kPerProject] = true\n    }\n    if (writeResult) {\n      registration[kWriteResult] = writeResult\n    }\n    pending.add(registration)\n    return registration\n  }\n\n  bench.from = <Name extends string>(nameOrFunction: Name | Function, source: string | BenchFromSource): BenchRegistration<Name> => {\n    validateBenchmarkProject(config)\n    if (typeof nameOrFunction !== 'string' && typeof nameOrFunction !== 'function') {\n      throw new TypeError('`bench.from()` requires a name (string or named function) as its first argument.')\n    }\n    if (typeof source !== 'string' && typeof source !== 'function') {\n      throw new TypeError('`bench.from()` expects a string path or a function returning the result data as its second argument.')\n    }\n    const name = (typeof nameOrFunction === 'function' ? nameOrFunction.name || '<anonymous>' : nameOrFunction) as Name\n    const registration: FromRegistration<Name> = {\n      [kRegistration]: true,\n      [kFromSource]: source,\n      name,\n      run: () => {\n        pending.delete(registration)\n        return runFrom(name, source)\n      },\n    }\n    pending.add(registration)\n    return registration\n  }\n","sourceCodeStart":419,"sourceCodeEnd":455,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/src/runtime/benchmark.ts#L419-L455","documentation":"`bench.from(name, source)` loads a benchmark result from a stored baseline (a file path or a factory function) instead of running it live. The first argument identifies the benchmark and must be a string name or a named function (its `.name` is used). This `TypeError` is thrown at registration time, before any baseline data is read, to fail fast on malformed calls. See `benchmark.ts:437`.","triggerScenarios":"Calling `bench.from()` with zero arguments; passing a number, boolean, object, or `undefined` as the first argument; passing only the source path and omitting the name; destructuring incorrectly so `undefined` is passed.","commonSituations":"Refactoring a `bench()` call into `bench.from()` and forgetting the name slot; passing the JSON result path as the first argument by mistake; copy-paste from `bench(name, fn)` leaving the name off when switching to `.from`.","solutions":["Pass a descriptive string name as the first argument: `bench.from('my-bench', source)`.","Or pass a named function whose `.name` becomes the benchmark name: `bench.from(function myBench() {}, source)`.","If you only have a path and no name intent, add an explicit string label so the comparison table is readable."],"exampleFix":"// before\nbench.from('/results/sort.bench.json')\n\n// after\nbench.from('sort', '/results/sort.bench.json')","handlingStrategy":"type-guard","validationCode":"// Before calling bench.from, validate the name argument\nfunction validBenchName(name: unknown): name is string | Function {\n  return typeof name === 'string' || typeof name === 'function'\n}\nif (!validBenchName(nameArg)) {\n  throw new Error('bench.from needs a string or named function name')\n}\nconst reg = bench.from(nameArg as any, source)","typeGuard":"function isBenchName(v: unknown): v is string | Function {\n  return typeof v === 'string' || typeof v === 'function'\n}","tryCatchPattern":"try {\n  const reg = bench.from(name, source)\n} catch (e) {\n  if (e instanceof TypeError && /requires a name/.test(e.message)) {\n    // fix the name argument and retry\n  }\n  throw e\n}","preventionTips":["Always pass an explicit string name as the first argument to bench.from.","Enable TypeScript strict mode so missing arguments are caught at compile time.","Write a thin wrapper that asserts the name type before delegating to bench.from."],"tags":["benchmark","bench-from","api-misuse","validation","typeerror"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}