{"id":"e29218c868451c1c","repo":"vitest-dev/vitest","slug":"bench-from-expects-a-string-path-or-a-function","errorCode":null,"errorMessage":"`bench.from()` expects a string path or a function returning the result data as its second argument.","messagePattern":"`bench\\.from\\(\\)` expects a string path or a function returning the result data as its second argument\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/runtime/benchmark.ts","lineNumber":440,"sourceCode":"      },\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\n  bench.compare = async (...args) => {\n    validateBenchmarkProject(config)\n","sourceCodeStart":422,"sourceCodeEnd":458,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/src/runtime/benchmark.ts#L422-L458","documentation":"`bench.from(name, source)` requires a `source` as its second argument: either a file path string (resolved relative to project root, with `${projectName}` substitution) or a function returning the baseline `BaselineData`. This `TypeError` is thrown at registration time when the source is missing or is not a string/function. See `benchmark.ts:440`.","triggerScenarios":"Calling `bench.from('name')` with one argument; passing a number/object/`Uint8Array` as source; passing the already-parsed result object directly instead of a path or factory.","commonSituations":"Assuming `bench.from` takes the data object directly; forgetting the source after renaming; passing a `BenchResult` object (which is not a function/string).","solutions":["Pass a file path string: `bench.from('sort', './baselines/sort.json')`.","Or pass a factory function: `bench.from('sort', () => storedResult)`.","If you have raw data, wrap it in a closure — `() => data` — rather than passing the object."],"exampleFix":"// before\nbench.from('sort', dataObject)\n\n// after\nbench.from('sort', '/baselines/sort.json')\n// or\nbench.from('sort', () => dataObject)","handlingStrategy":"type-guard","validationCode":"function isBenchSource(s: unknown): s is string | (() => any) {\n  return typeof s === 'string' || typeof s === 'function'\n}\nif (!isBenchSource(sourceArg)) {\n  throw new Error('bench.from source must be a path string or factory function')\n}\nconst reg = bench.from(name, sourceArg)","typeGuard":"function isBenchFromSource(v: unknown): v is string | ((() => BaselineData) | (() => Promise<BaselineData>)) {\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 && /expects a string path or a function/.test(e.message)) {\n    // wrap raw data in a factory: () => data\n  }\n  throw e\n}","preventionTips":["If you have a BaselineData object, wrap it: bench.from(name, () => data).","Keep baseline files at known paths and pass those path strings directly.","Use TypeScript overloads so the source type is enforced at compile time."],"tags":["benchmark","bench-from","api-misuse","validation","typeerror"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}