vitest-dev/vitest · error · Error

Test specification for task ${id} was not found

Error message

Test specification for task ${id} was not found

What it means

rerunTask(id) (core.ts:1286) finds the task in idMap but state.getReportedEntityById(id) then returns null, meaning the task cannot be converted into a TestSpecification. The entity exists in the flat id map but isn't a runnable reported entity (e.g. a container/suite node without a usable spec).

Source

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

      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 d568f8ce37)

Solutions

  1. Rerun at the file level (rerunFiles) instead of an arbitrary task id.
  2. Rebuild state (re-discover files) before rerunning.
  3. Filter ids to leaf test entities obtained from getReportedEntityById.

Example fix

// before
await vitest.rerunTask(suiteId) // suite has no spec

// after
await vitest.rerunFiles([task.file.filepath])
Defensive patterns

Strategy: validation

Validate before calling

const entity = vitest.state.getReportedEntityById(id)
if (!entity || typeof entity.toTestSpecification !== 'function') {
  throw new Error(`No runnable spec for task ${id}; rerun at file level`)
}
await vitest.rerunTask(id)

Type guard

function isRunnableEntity(entity: any): boolean {
  return entity != null && typeof entity.toTestSpecification === 'function'
}

Prevention

When it happens

Trigger: Rerunning an id that resolves to a non-test entity, or a state desync where the reported-entity map wasn't rebuilt for that id.

Common situations: Internal state desync after partial file reloads; rerunning a suite/container id that has no direct TestSpecification; custom watcher integrations passing non-leaf ids.

Related errors


AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03). Data as JSON: /data/errors/5969ce8c713955de.json. Report an issue: GitHub.