vitest-dev/vitest · error · TypeError
`bench.from()` requires a name (string or named function) as
Error message
`bench.from()` requires a name (string or named function) as its first argument.
What it means
`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`.
Source
Thrown at packages/vitest/src/runtime/benchmark.ts:437
run: (options?: BenchRunOptions) => {
pending.delete(registration)
return runSingle(name, fn, fnOpts, options, meta, writeResult)
},
}
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
}
View on GitHub (pinned to d568f8ce37)
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.
Example fix
// before
bench.from('/results/sort.bench.json')
// after
bench.from('sort', '/results/sort.bench.json') Defensive patterns
Strategy: type-guard
Validate before calling
// Before calling bench.from, validate the name argument
function validBenchName(name: unknown): name is string | Function {
return typeof name === 'string' || typeof name === 'function'
}
if (!validBenchName(nameArg)) {
throw new Error('bench.from needs a string or named function name')
}
const reg = bench.from(nameArg as any, source) Type guard
function isBenchName(v: unknown): v is string | Function {
return typeof v === 'string' || typeof v === 'function'
} Try / catch
try {
const reg = bench.from(name, source)
} catch (e) {
if (e instanceof TypeError && /requires a name/.test(e.message)) {
// fix the name argument and retry
}
throw e
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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`.
Related errors
- `bench.from()` expects a string path or a function returning
- `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/1b1faa152f3b429c.json.
Report an issue: GitHub.