vitest-dev/vitest · error
Task was not found
Error message
Task ${id} was not found What it means
rerunTask looks up the task by id in state.idMap; if the id was never collected/registered it throws. This is a watcher-driven API, so the error means an id was supplied that Vitest never indexed — typically a stale, fabricated, or post-deletion id.
Solutions
- Guard with state.idMap.has(id) (or state.getTask(id)) before calling rerunTask.
- Re-run collection so the idMap is rebuilt before rerunning.
- Ensure the source file still exists and was imported successfully in the last run.
Example fix
// before
await vitest.rerunTask(id)
// after
if (!vitest.state.idMap.has(id)) {
await vitest.collect() // rebuild idMap
}
if (vitest.state.idMap.has(id)) {
await vitest.rerunTask(id)
} Defensive patterns
Strategy: validation
Validate before calling
function taskExists(state, id) {
return state.idMap.has(id)
}
// before: if (!taskExists(vitest.state, id)) await vitest.collect() Type guard
function isKnownTaskId(state, id): id is string {
return typeof id === 'string' && state.idMap.has(id)
} Prevention
- Always (re)collect before rerunning tasks so the idMap is fresh.
- Never persist task ids across runs; ids are run-scoped.
- Guard rerunTask callers (watcher/IPC handlers) with state.idMap.has(id).
When it happens
Trigger: Calling rerunTask(id) (usually via watcher/IPC/programmatic API) with an id that is not a key in state.idMap, e.g. after the source file was removed, after state was cleared, or with a hardcoded/external id.
Common situations: Watcher rerun after file deletion; programmatic API misuse with hardcoded ids; stale id captured from a previous run; rerun triggered during teardown.
Related errors
AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11).
Data as JSON: /api/errors/a2fcf1afa2133f04.
Report an issue: GitHub.
Appendix: source
Thrown at packages/vitest/src/node/core.ts:1279
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 1fa9837ec2)