vitest-dev/vitest · error · Error
task " " was not defined
Error message
task "${name}" was not defined What it means
`bench.compare(...)` returns a `BenchStorage<T>` whose `.get(name)` looks up results by registration name. This error fires when `get` is called with a name that matches neither a stored baseline (from `bench.from`) nor a freshly-run result. Names are the literal string passed to `bench(name, ...)` or `bench.from(name, ...)` (or the function's `.name`).
Solutions
- Call `.get()` only with names that were registered: the string passed to `bench(name, ...)` or `bench.from(name, ...)`.
- Use the typed `BenchStorage<T>` — TypeScript will restrict `.get()` to the union of registered names derived from the `bench.compare(...)` arguments.
- Avoid anonymous functions as benchmark names; when passing a function, ensure it has a stable `.name` (no minification stripping).
Example fix
// before
const a = bench('sort', fn)
const storage = await bench.compare(a, b)
storage.get('sorted') // typo
// after
storage.get('sort') Defensive patterns
Strategy: validation
Validate before calling
const valid = new Set([...registrations].map(r => r.name)); if (!valid.has(requestedName)) throw new Error(`'${requestedName}' not registered; valid: ${[...valid].join(', ')}`) Type guard
function isKnownName<T extends string>(name: string, set: Set<T>): name is T { return set.has(name as T) } Try / catch
try { storage.get(name) } catch (e) { if (/was not defined/.test(e.message)) { console.warn('available:', availableNames); return } throw e } Prevention
- Derive lookup names from the registration's `.name` field, not a hand-typed string.
- Let TypeScript's `BenchStorage<T>` union narrow the `.get()` argument.
- Avoid anonymous functions as benchmark names to keep `.name` stable.
When it happens
Trigger: Calling `storage.get('wrong-name')` after `const storage = await bench.compare(regA, regB)` where `regA.name === 'a'` and `regB.name === 'b'`. The lookup at benchmark.ts:272-276 checks `fromResults?.get(name)` then `results.get(name)` and throws if both miss.
Common situations: Renaming a benchmark but forgetting to update the `.get(name)` consumer; using the function reference's `.name` which is minified/empty in a bundled context; passing the registration object instead of its `.name` string to `.get()`.
Related errors
- `bench.compare()` expects every argument to be the return…
- `bench.compare()` requires at least 2 benchmarks, received
- `bench()` expects a benchmark function. Call `bench(name…
- `bench.from()` expects a string path or a function…
- `bench.from()` requires a name (string or named function)…
AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11).
Data as JSON: /api/errors/e78d4340863a7804.
Report an issue: GitHub.
Appendix: source
Thrown at packages/vitest/src/runtime/benchmark.ts:274
period: data.period,
totalTime: data.totalTime,
rank: 0,
fromStore: true,
})
const createCompareStorage = <T extends string>(
results: Map<string, BenchResult>,
fromResults?: Map<string, BaselineData>,
): BenchStorage<T> => {
return {
get(name: T) {
const stored = fromResults?.get(name)
if (stored) {
return stored as BenchResult
}
const result = results.get(name)
if (!result) {
throw new Error(`task "${name}" was not defined`)
}
return result
},
}
}
interface TaskMeta { perProject?: true }
const serializeBenchmark = (
results: BenchResult[],
name: string,
taskMeta?: Map<string, TaskMeta>,
fromTasks?: TestBenchmarkTask[],
): TestBenchmark => {
const tasks: TestBenchmarkTask[] = results.map(result => ({
name: result.name,
latency: result.latency,
throughput: result.throughput,View on GitHub (pinned to 1fa9837ec2)