vitest-dev/vitest · error · Error

Cannot find the environment. This is a bug in Vitest.

Error message

Cannot find the environment. This is a bug in Vitest.

What it means

Internal assertion thrown when a resolved TestSpecification has no matching ContextTestEnvironment in the environments WeakMap built by getSpecificationsOptions. The environments map is populated during spec collection, so a missing entry means the spec/environment resolution pipeline produced an entry it cannot service — this is not a user-facing config error but a framework invariant violation.

Source

Thrown at packages/vitest/src/node/pool.ts:119

      const taskGroup: PoolTask[] = []
      const browserSpecs: TestSpecification[] = []
      taskGroups.push({
        tasks: taskGroup,
        maxWorkers: group.maxWorkers,
        browserSpecs,
      })

      for (const specs of group.specs) {
        const { project, pool } = specs[0]
        if (pool === 'browser') {
          browserSpecs.push(...specs)
          continue
        }

        const environment = environments.get(specs[0])!
        if (!environment) {
          throw new Error(`Cannot find the environment. This is a bug in Vitest.`)
        }

        let env = projectEnvs.get(project)
        if (!env) {
          env = {
            ...process.env,
            ...options.env,
            ...ctx.config.env,
            ...project.config.env,
          }

          // V8 serializes compile-cached scripts without the source positions
          // that precise coverage relies on, so the compile cache must stay off
          // for the v8 provider (and custom providers, whose mechanism we can't
          // assume) in workers and any process they spawn. istanbul instruments
          // the source at transform time, so the cache is harmless there and the
          // boot speedup is kept.
          if (ctx.config.coverage.enabled && ctx.config.coverage.provider !== 'istanbul') {

View on GitHub (pinned to d568f8ce37)

Solutions

  1. Report as a Vitest bug with a minimal reproduction (the message itself says so).
  2. Downgrade to the last working Vitest version to confirm it is a regression.
  3. Disable any custom environment plugin to isolate whether a third-party environment is involved.
  4. Clear node_modules/.vite and reinstall to rule out a stale build artifact.
Defensive patterns

Strategy: try-catch

Try / catch

// This is an internal assertion; surface it with context for a bug report
try {
  await vitest.start()
} catch (e) {
  if (e.message === 'Cannot find the environment. This is a bug in Vitest.') {
    console.error('Vitest internal bug — please report with reproduction.')
    console.error('Vitest version:', require('vitest/package.json').version)
  }
  throw e
}

Prevention

When it happens

Trigger: An internal divergence where getSpecificationsOptions returns an environments WeakMap that does not contain a key for specs[0] during task grouping in createPool (packages/vitest/src/node/pool.ts:117-119). Typically only reproducible with custom environment plugins that mishandle spec resolution or across a Vitest version regression.

Common situations: Upgrading Vitest to a version with a regression in environment resolution; a custom environment provider that throws during initialization leaving the WeakMap partially populated; race conditions during watch-mode re-collection.

Related errors


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