vitest-dev/vitest · error · Error

Cannot find environment for ${testFilePath}

Error message

Cannot find environment for ${testFilePath}

What it means

getModuleGraph (graph.ts:30-32) resolves which Vite DevEnvironment should own a test file and throws if none is found. The environment is selected from project.vite.environments either by explicit viteEnvironment, the native __vitest__ environment, or getTestFileEnvironment(project, path, browser); a falsy result means no environment matches the file.

Source

Thrown at packages/vitest/src/utils/graph.ts:31

  const externalized = new Set<string>()
  const inlined = new Set<string>()

  const project = ctx.getProjectByName(projectName)
  const browser = project.config.browser.enabled

  let environment: DevEnvironment | undefined

  if (viteEnvironment) {
    environment = project.vite.environments[viteEnvironment]
  }
  else {
    environment = project.config.experimental.viteModuleRunner === false
      ? project.vite.environments.__vitest__
      : getTestFileEnvironment(project, testFilePath, browser)
  }

  if (!environment) {
    throw new Error(`Cannot find environment for ${testFilePath}`)
  }
  const seen = new Map<EnvironmentModuleNode, string>()

  function get(mod?: EnvironmentModuleNode) {
    if (!mod || !mod.id) {
      return
    }
    if (
      mod.id === '\0vitest/browser'
      // the export helper is injected in all vue files
      // so the module graph becomes too bouncy
      || mod.id.includes('plugin-vue:export-helper')
    ) {
      return
    }
    if (seen.has(mod)) {
      return seen.get(mod)
    }

View on GitHub (pinned to d568f8ce37)

Solutions

  1. Verify the viteEnvironment name exists in project.vite.environments (or omit it to let Vitest pick).
  2. Ensure browser.enabled is consistent with the environment the test file expects.
  3. If using experimental.viteModuleRunner:false, confirm the __vitest__ environment is initialized.
  4. Check that environment plugins (e.g. @vitest/browser) are correctly registered.

Example fix

// before: requesting graph with a non-existent environment name
await getModuleGraph(ctx, project, testFile, 'happy-dom')

// after: omit the name or use a registered one
await getModuleGraph(ctx, project, testFile) // let vitest resolve
Defensive patterns

Strategy: validation

Validate before calling

function assertEnvironmentRegistered(project: { vite: { environments: Record<string, unknown> } }, name?: string) {
  if (name && !(name in project.vite.environments)) {
    throw new Error(`Vite environment '${name}' is not registered`)
  }
}

Type guard

function isRegisteredEnvironment(
  project: { vite: { environments: Record<string, unknown> } },
  name: string
): boolean {
  return name in project.vite.environments
}

Prevention

When it happens

Trigger: Requesting the module graph (used by the UI, browser pool, and dependency-externalization) for a test file whose environment is not registered — e.g. an unknown custom viteEnvironment name, a browser-disabled project with an environment key that does not exist, or viteModuleRunner:false when the __vitest__ environment was never created.

Common situations: Passing an invalid viteEnvironment name to the API; a plugin/setup that failed to register the environment; mismatch between the environment name in test config and the Vite environments map; using the browser reporter on a project whose browser environment is disabled.

Related errors


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