{"record":{"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":"validation","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/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/runtime/benchmark.ts#L422-L458","documentation":"The second argument to `bench.from(name, source)` must be either a string path (resolved against project root with `${projectName}` substitution) or a function returning baseline data. This `TypeError` fires when `source` is any other type. The guard is at benchmark.ts:439-441.","triggerScenarios":"Calling `bench.from('name', 123)`, `bench.from('name', null)`, `bench.from('name', { latency: ... })` (object instead of a function), or `bench.from('name', undefined)`. Only `string` or `function` are accepted.","commonSituations":"Passing the baseline data object directly instead of a function returning it; passing a registration object; refactor removing the source argument.","solutions":["Pass a path string: `bench.from('baseline', './results/baseline.json')`.","Or pass a function returning the data: `bench.from('baseline', () => baselineData)`.","If you have a raw object, wrap it: `bench.from('baseline', () => ({ latency, throughput, period, totalTime }))`."],"exampleFix":"// before\nbench.from('baseline', rawDataObject)\n\n// after\nbench.from('baseline', () => rawDataObject)","handlingStrategy":"validation","validationCode":"if (typeof source !== 'string' && typeof source !== 'function') throw new TypeError('source must be string path or function')","typeGuard":"function isFromSource(v: unknown): v is string | (() => any) { return typeof v === 'string' || typeof v === 'function' }","tryCatchPattern":"null","preventionTips":["Keep baseline paths as typed string constants.","When passing inline data, wrap in `() => data`.","Type the second argument as `string | BenchFromSource`."],"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"}