vitest-dev/vitest · error · Error

Task ${id} was not found

Error message

Task ${id} was not found

What it means

rerunTask(id) (core.ts:1281) is an internal watcher entry point. It looks the id up in state.idMap and throws if no task with that id was ever registered. The id must come from vitest's own task state, not from user code.

Source

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

      files = files.filter(file => filteredFiles.some(f => f.moduleId === file))
    }

    const specifications = files.flatMap(file => this.getModuleSpecifications(file))
    await Promise.all([
      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(

View on GitHub (pinned to d568f8ce37)

Solutions

  1. Use ids obtained from vitest.state.idMap / state.getReportedEntityById, never hand-rolled strings.
  2. Ensure tasks are loaded (files discovered) before invoking a rerun.
  3. Prefer the public file-level rerun APIs (rerunFiles / changeNamePattern) over the internal rerunTask.

Example fix

// before
vitest.rerunTask(someIdFromUnknownSource)

// after
const task = vitest.state.idMap.get(someId)
if (task) await vitest.rerunTask(someId)
Defensive patterns

Strategy: validation

Validate before calling

if (!vitest.state.idMap.has(id)) {
  throw new Error(`Refusing to rerun unknown task id: ${id}`)
}
await vitest.rerunTask(id)

Type guard

function taskExists(vitest: Vitest, id: string): boolean {
  return vitest.state.idMap.has(id)
}

Prevention

When it happens

Trigger: Watch-mode rerun triggered for a task id not present in state.idMap — e.g. after the task's file was deleted, after state was cleared, or from a stale externally-supplied id.

Common situations: Calling the internal rerunTask directly with an arbitrary string; rerunning a task whose file was just removed; out-of-sync state in a custom watcher integration.

Related errors


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