{"record":{"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":"warning","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/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/utils/graph.ts#L13-L49","documentation":"getModuleGraph resolves the Vite DevEnvironment for a test file: it uses the explicitly passed viteEnvironment, else the __vitest__ environment when experimental.viteModuleRunner is false, else getTestFileEnvironment(project, file, browser). If none of these yields an environment, it throws 'Cannot find environment for <file>'. This runs when the UI/API requests the module graph for a file.","triggerScenarios":"A request for a module graph (e.g. clicking a file in the Vitest UI, or the API) for a test file whose Vite environment is not registered. Common when browser.enabled but no browser environment is configured, or viteModuleRunner:false is set without the __vitest__ environment being available.","commonSituations":"Opening the Vitest UI for a project with browser testing misconfigured, a custom Vite environment that is not registered for the file's environment name, or a stale project state after toggling experimental flags.","solutions":["Verify browser config (test.browser.enabled with a valid headless provider) if the file is a browser test.","If experimental.viteModuleRunner is false, ensure the __vitest__ environment is registered in the Vite config.","Check that test.environmentName / per-project environment is one Vite knows about.","Restart Vitest after changing environment config so the project's environments map is rebuilt."],"exampleFix":"// before: browser tests but no browser environment registered\nexport default defineConfig({ test: { browser: { enabled: true } } })\n\n// after\nexport default defineConfig({\n  test: { browser: { enabled: true, instances: [{ browser: 'chromium' }] }, projects: [...] }\n})","handlingStrategy":"validation","validationCode":"const env = viteEnvironment\n  ? project.vite.environments[viteEnvironment]\n  : getTestFileEnvironment(project, file, browser)\nif (!env) throw new Error('no Vite environment registered for ' + file)","typeGuard":"function hasEnvironment(project, name) {\n  return Boolean(project.vite.environments[name])\n}","tryCatchPattern":null,"preventionTips":["Register all environments you reference in config.","Keep browser/environment config consistent with the projects list.","Restart Vitest after environment config changes."],"tags":["module-graph","vite","environment","browser","ui"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}