{"record":{"id":"e78d4340863a7804","repo":"vitest-dev/vitest","slug":"task-name-was-not-defined","errorCode":null,"errorMessage":"task \"${name}\" was not defined","messagePattern":"task \"(.+?)\" was not defined","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/runtime/benchmark.ts","lineNumber":274,"sourceCode":"    period: data.period,\n    totalTime: data.totalTime,\n    rank: 0,\n    fromStore: true,\n  })\n\n  const createCompareStorage = <T extends string>(\n    results: Map<string, BenchResult>,\n    fromResults?: Map<string, BaselineData>,\n  ): BenchStorage<T> => {\n    return {\n      get(name: T) {\n        const stored = fromResults?.get(name)\n        if (stored) {\n          return stored as BenchResult\n        }\n        const result = results.get(name)\n        if (!result) {\n          throw new Error(`task \"${name}\" was not defined`)\n        }\n        return result\n      },\n    }\n  }\n\n  interface TaskMeta { perProject?: true }\n\n  const serializeBenchmark = (\n    results: BenchResult[],\n    name: string,\n    taskMeta?: Map<string, TaskMeta>,\n    fromTasks?: TestBenchmarkTask[],\n  ): TestBenchmark => {\n    const tasks: TestBenchmarkTask[] = results.map(result => ({\n      name: result.name,\n      latency: result.latency,\n      throughput: result.throughput,","sourceCodeStart":256,"sourceCodeEnd":292,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/runtime/benchmark.ts#L256-L292","documentation":"`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`).","triggerScenarios":"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.","commonSituations":"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()`.","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)."],"exampleFix":"// before\nconst a = bench('sort', fn)\nconst storage = await bench.compare(a, b)\nstorage.get('sorted') // typo\n\n// after\nstorage.get('sort')","handlingStrategy":"validation","validationCode":"const valid = new Set([...registrations].map(r => r.name)); if (!valid.has(requestedName)) throw new Error(`'${requestedName}' not registered; valid: ${[...valid].join(', ')}`)","typeGuard":"function isKnownName<T extends string>(name: string, set: Set<T>): name is T { return set.has(name as T) }","tryCatchPattern":"try { storage.get(name) } catch (e) { if (/was not defined/.test(e.message)) { console.warn('available:', availableNames); return } throw e }","preventionTips":["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."],"tags":["benchmark","compare","validation","lookup"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}