{"id":"8758b46771599e50","repo":"vitest-dev/vitest","slug":"task-instance-was-not-found-for-runnertask-type","errorCode":null,"errorMessage":"Task instance was not found for ${runnerTask.type} \"${runnerTask.name}\"","messagePattern":"Task instance was not found for (.+?) \"(.+?)\"","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/node/reporters/reported-tasks.ts","lineNumber":779,"sourceCode":"   */\n  readonly workerId: number\n}\n\nfunction storeTask(\n  project: TestProject,\n  runnerTask: RunnerTask,\n  reportedTask: TestCase | TestSuite | TestModule,\n): void {\n  project.vitest.state.reportedTasksMap.set(runnerTask, reportedTask)\n}\n\nfunction getReportedTask(\n  project: TestProject,\n  runnerTask: RunnerTask,\n): TestCase | TestSuite | TestModule {\n  const reportedTask = project.vitest.state.getReportedEntity(runnerTask)\n  if (!reportedTask) {\n    throw new Error(\n      `Task instance was not found for ${runnerTask.type} \"${runnerTask.name}\"`,\n    )\n  }\n  return reportedTask\n}\n\nfunction getSuiteState(task: RunnerTestSuite | RunnerTestFile): TestSuiteState {\n  const mode = task.mode\n  const state = task.result?.state\n  if (mode === 'skip' || mode === 'todo' || state === 'skip' || state === 'todo') {\n    return 'skipped'\n  }\n  if (state == null || state === 'run' || state === 'only') {\n    return 'pending'\n  }\n  if (state === 'fail') {\n    return 'failed'\n  }","sourceCodeStart":761,"sourceCodeEnd":797,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/src/node/reporters/reported-tasks.ts#L761-L797","documentation":"Thrown by `getReportedTask` (reported-tasks.ts:779) when a runner task (the low-level `RunnerTask` from the test runner) is looked up in `project.vitest.state.reportedTasksMap` / via `getReportedEntity` but no reported entity has been registered for it. This is an internal invariant: every runner task that the reporter layer processes must have been stored first via `storeTask`. Encountering it indicates a state-tracking bug or out-of-order access inside Vitest rather than a user configuration problem.","triggerScenarios":"A custom reporter calling APIs that walk runner tasks before the reported-task map is populated; an internal ordering change where a task event arrives before its `storeTask` call; mutating or reusing runner task objects across Vitest instances.","commonSituations":"Building a custom reporter that touches `experimental_getRunnerTask` / internal task state at the wrong lifecycle hook; running against an in-development Vitest build with a regression in task tracking.","solutions":["If you hit this in app code, report a Vitest bug with a minimal reproduction (it signals an internal consistency violation).","In custom reporters, only access reported tasks during/after the hooks that guarantee they exist (e.g. `onTestRunEnd`, `onTaskUpdate`) rather than early lifecycle hooks.","Upgrade to the latest patch release; this kind of invariant break is typically fixed quickly."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  const entity = project.vitest.state.getReportedEntity(runnerTask)\n  if (!entity) return // guard instead of letting Vitest throw\n  // ... use entity\n} catch (e) {\n  if (e instanceof Error && /Task instance was not found/.test(e.message)) {\n    // skip tasks without reported entities\n  } else throw e\n}","preventionTips":["In custom reporters, prefer the reported-task APIs handed to your hooks over re-deriving entities from runner tasks.","Only access reported entities during hooks that guarantee their existence.","Guard with `state.getReportedEntity(task)` truthiness before use."],"tags":["reporter","internal","tasks","invariant"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}