vitest-dev/vitest · error · TypeError
`bench.from()` expects a string path or a function returning
Error message
`bench.from()` expects a string path or a function returning the result data as its second argument.
What it means
`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`.
Source
Thrown at packages/vitest/src/runtime/benchmark.ts:440
},
}
if (perProject) {
registration[kPerProject] = true
}
if (writeResult) {
registration[kWriteResult] = writeResult
}
pending.add(registration)
return registration
}
bench.from = <Name extends string>(nameOrFunction: Name | Function, source: string | BenchFromSource): BenchRegistration<Name> => {
validateBenchmarkProject(config)
if (typeof nameOrFunction !== 'string' && typeof nameOrFunction !== 'function') {
throw new TypeError('`bench.from()` requires a name (string or named function) as its first argument.')
}
if (typeof source !== 'string' && typeof source !== 'function') {
throw new TypeError('`bench.from()` expects a string path or a function returning the result data as its second argument.')
}
const name = (typeof nameOrFunction === 'function' ? nameOrFunction.name || '<anonymous>' : nameOrFunction) as Name
const registration: FromRegistration<Name> = {
[kRegistration]: true,
[kFromSource]: source,
name,
run: () => {
pending.delete(registration)
return runFrom(name, source)
},
}
pending.add(registration)
return registration
}
bench.compare = async (...args) => {
validateBenchmarkProject(config)
View on GitHub (pinned to d568f8ce37)
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.
Example fix
// before
bench.from('sort', dataObject)
// after
bench.from('sort', '/baselines/sort.json')
// or
bench.from('sort', () => dataObject) Defensive patterns
Strategy: type-guard
Validate before calling
function isBenchSource(s: unknown): s is string | (() => any) {
return typeof s === 'string' || typeof s === 'function'
}
if (!isBenchSource(sourceArg)) {
throw new Error('bench.from source must be a path string or factory function')
}
const reg = bench.from(name, sourceArg) Type guard
function isBenchFromSource(v: unknown): v is string | ((() => BaselineData) | (() => Promise<BaselineData>)) {
return typeof v === 'string' || typeof v === 'function'
} Try / catch
try {
const reg = bench.from(name, source)
} catch (e) {
if (e instanceof TypeError && /expects a string path or a function/.test(e.message)) {
// wrap raw data in a factory: () => data
}
throw e
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Assuming `bench.from` takes the data object directly; forgetting the source after renaming; passing a `BenchResult` object (which is not a function/string).
Related errors
- `bench.from()` requires a name (string or named function) as
- `bench.compare()` expects every argument to be the return va
- `bench()` does not accept options as the third argument. Pas
- `bench()` expects a benchmark function. Call `bench(name, fn
- `bench.from()` could not find a result file at "${resolved}"
AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03).
Data as JSON: /data/errors/e29218c868451c1c.json.
Report an issue: GitHub.