{"record":{"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":"validation","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/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/runtime/benchmark.ts#L419-L455","documentation":"`bench.from(nameOrFunction, source)` requires its first argument to be a string or a function (the function's `.name` is used as the benchmark name). This `TypeError` fires when the first argument is any other type — number, object, null, etc. The check is at benchmark.ts:436-438.","triggerScenarios":"Calling `bench.from(123, source)`, `bench.from({}, source)`, `bench.from(null, source)`, or `bench.from(undefined, source)`. The guard `typeof nameOrFunction !== 'string' && typeof nameOrFunction !== 'function'` is narrow and explicit.","commonSituations":"Passing an options object as the first argument by mistake (mirroring `bench(name, options, fn)` shape that does not exist for `.from`); passing a registration object instead of a name; refactor where a name variable became `undefined`.","solutions":["Pass a literal string name: `bench.from('baseline', source)`.","Or pass a named function declaration/expression: `bench.from(function baseline() {}, source)` — its `.name` is used.","If the name is dynamic, ensure it is typed as `string` before the call; add a runtime check `if (typeof name !== 'string') throw ...` upstream."],"exampleFix":"// before\nbench.from({ name: 'baseline' }, './results.json')\n\n// after\nbench.from('baseline', './results.json')","handlingStrategy":"validation","validationCode":"if (typeof name !== 'string' && typeof name !== 'function') throw new TypeError('name must be string or function')","typeGuard":"function isNameable(v: unknown): v is string | Function { return typeof v === 'string' || typeof v === 'function' }","tryCatchPattern":"null","preventionTips":["Always pass a string literal name to `bench.from`.","If passing a function, ensure it is a named function declaration/expression.","Type the call site so `nameOrFunction: string | Function` is enforced."],"tags":["benchmark","bench-from","api-usage","validation"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}