{"id":"9ec1b923fca0d906","repo":"vitest-dev/vitest","slug":"cannot-find-environment-for-testfilepath","errorCode":null,"errorMessage":"Cannot find environment for ${testFilePath}","messagePattern":"Cannot find environment for (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/utils/graph.ts","lineNumber":31,"sourceCode":"  const externalized = new Set<string>()\n  const inlined = new Set<string>()\n\n  const project = ctx.getProjectByName(projectName)\n  const browser = project.config.browser.enabled\n\n  let environment: DevEnvironment | undefined\n\n  if (viteEnvironment) {\n    environment = project.vite.environments[viteEnvironment]\n  }\n  else {\n    environment = project.config.experimental.viteModuleRunner === false\n      ? project.vite.environments.__vitest__\n      : getTestFileEnvironment(project, testFilePath, browser)\n  }\n\n  if (!environment) {\n    throw new Error(`Cannot find environment for ${testFilePath}`)\n  }\n  const seen = new Map<EnvironmentModuleNode, string>()\n\n  function get(mod?: EnvironmentModuleNode) {\n    if (!mod || !mod.id) {\n      return\n    }\n    if (\n      mod.id === '\\0vitest/browser'\n      // the export helper is injected in all vue files\n      // so the module graph becomes too bouncy\n      || mod.id.includes('plugin-vue:export-helper')\n    ) {\n      return\n    }\n    if (seen.has(mod)) {\n      return seen.get(mod)\n    }","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/src/utils/graph.ts#L13-L49","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the viteEnvironment name exists in project.vite.environments (or omit it to let Vitest pick).","Ensure browser.enabled is consistent with the environment the test file expects.","If using experimental.viteModuleRunner:false, confirm the __vitest__ environment is initialized.","Check that environment plugins (e.g. @vitest/browser) are correctly registered."],"exampleFix":"// before: requesting graph with a non-existent environment name\nawait getModuleGraph(ctx, project, testFile, 'happy-dom')\n\n// after: omit the name or use a registered one\nawait getModuleGraph(ctx, project, testFile) // let vitest resolve","handlingStrategy":"validation","validationCode":"function assertEnvironmentRegistered(project: { vite: { environments: Record<string, unknown> } }, name?: string) {\n  if (name && !(name in project.vite.environments)) {\n    throw new Error(`Vite environment '${name}' is not registered`)\n  }\n}","typeGuard":"function isRegisteredEnvironment(\n  project: { vite: { environments: Record<string, unknown> } },\n  name: string\n): boolean {\n  return name in project.vite.environments\n}","tryCatchPattern":null,"preventionTips":["Omit viteEnvironment in getModuleGraph to let Vitest auto-resolve.","Register all custom Vite environments before requesting module graphs.","Keep browser.enabled consistent with the environments you query."],"tags":["module-graph","environment","vite","browser"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}