vitest-dev/vitest · error

Test specification for task

Error message

Test specification for task ${id} was not found

What it means

After the task is found in idMap, getReportedEntityById(id) must return a reported entity convertible to a spec. If it returns null, the task exists internally but has no reported entity (e.g. reporting hasn't run, entities were cleared, or a mid-watch race). The message signals an inconsistency between idMap and reported entities.

Solutions

  1. Ensure onInit/reporting has run so reported entities are built before rerunning.
  2. Avoid calling rerunTask during teardown or invalidation windows.
  3. Re-collect to rebuild reported entities, then rerun.

Example fix

// before
await vitest.rerunTask(id)

// after
const entity = vitest.state.getReportedEntityById(id)
if (!entity) {
  await vitest.collect()
}
if (vitest.state.getReportedEntityById(id)) {
  await vitest.rerunTask(id)
}
Defensive patterns

Strategy: validation

Validate before calling

function hasReportedEntity(state, id) {
  return state.getReportedEntityById(id) != null
}
// before: if (!hasReportedEntity(vitest.state, id)) await vitest.collect()

Prevention

When it happens

Trigger: rerunTask(id) where state.idMap.get(id) is truthy but state.getReportedEntityById(id) returns null/undefined — typically during teardown, state clear, or before onInit/reporting has built reported entities.

Common situations: State cleared mid-watch; task registered before its reported entity; race during file invalidation; calling rerunTask before the initial report cycle completes.

Related errors


AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11). Data as JSON: /api/errors/5969ce8c713955de. Report an issue: GitHub.

Appendix: source

Thrown at packages/vitest/src/node/core.ts:1284

      this.report('onWatcherRerun', files, trigger),
      ...this._onUserTestsRerun.map(fn => fn(specifications)),
    ])
    const testResult = await this.runFiles(specifications, allTestsRun)

    await this.report('onWatcherStart', this.state.getFiles(files))
    return testResult
  }

  /** @internal */
  async rerunTask(id: string): Promise<void> {
    const task = this.state.idMap.get(id)
    if (!task) {
      throw new Error(`Task ${id} was not found`)
    }

    const reportedTask = this.state.getReportedEntityById(id)
    if (!reportedTask) {
      throw new Error(`Test specification for task ${id} was not found`)
    }

    const specifications = [reportedTask.toTestSpecification()]
    await Promise.all([
      this.report(
        'onWatcherRerun',
        [task.file.filepath],
        'tasks' in task ? 'rerun suite' : 'rerun test',
      ),
      ...this._onUserTestsRerun.map(fn => fn(specifications)),
    ])
    await this.runFiles(specifications, false)
    await this.report(
      'onWatcherStart',
      ['module' in reportedTask ? reportedTask.module.task : reportedTask.task],
    )
  }

View on GitHub (pinned to 1fa9837ec2)