{"record":{"id":"a4d77eba04afbe2a","repo":"vitest-dev/vitest","slug":"vitest-failed-to-access-its-internal-state-one-o","errorCode":null,"errorMessage":"Vitest failed to access its internal state.\n\nOne of the following is possible:\n- \"vitest\" is imported directly without running \"vitest\" command\n- \"vitest\" is imported inside \"globalSetup\" (to fix this, use \"setupFiles\" instead, because \"globalSetup\" runs in a different context)\n- \"vitest\" is imported inside Vite / Vitest config file\n- Otherwise, it might be a Vitest bug. Please report it to https://github.com/vitest-dev/vitest/issues\n","messagePattern":"Vitest failed to access its internal state\\.\n\nOne of the following is possible:\n- \"vitest\" is imported directly without running \"vitest\" command\n- \"vitest\" is imported inside \"globalSetup\" \\(to fix this, use \"setupFiles\" instead, because \"globalSetup\" runs in a different context\\)\n- \"vitest\" is imported inside Vite / Vitest config file\n- Otherwise, it might be a Vitest bug\\. Please report it to https://github\\.com/vitest-dev/vitest/issues\n","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"packages/vitest/src/runtime/utils.ts","lineNumber":21,"sourceCode":"\nconst NAME_WORKER_STATE = '__vitest_worker__'\n\nexport class EnvironmentTeardownError extends Error {\n  name = 'EnvironmentTeardownError'\n}\n\nexport function getWorkerState(): WorkerGlobalState {\n  // @ts-expect-error untyped global\n  const workerState = globalThis[NAME_WORKER_STATE]\n  if (!workerState) {\n    const errorMsg\n      = 'Vitest failed to access its internal state.'\n        + '\\n\\nOne of the following is possible:'\n        + '\\n- \"vitest\" is imported directly without running \"vitest\" command'\n        + '\\n- \"vitest\" is imported inside \"globalSetup\" (to fix this, use \"setupFiles\" instead, because \"globalSetup\" runs in a different context)'\n        + '\\n- \"vitest\" is imported inside Vite / Vitest config file'\n        + '\\n- Otherwise, it might be a Vitest bug. Please report it to https://github.com/vitest-dev/vitest/issues\\n'\n    throw new Error(errorMsg)\n  }\n  return workerState\n}\n\nexport function getSafeWorkerState(): WorkerGlobalState | undefined {\n  // @ts-expect-error untyped global\n  return globalThis[NAME_WORKER_STATE]\n}\n\nexport function provideWorkerState(context: any, state: WorkerGlobalState): WorkerGlobalState {\n  Object.defineProperty(context, NAME_WORKER_STATE, {\n    value: state,\n    configurable: true,\n    writable: true,\n    enumerable: false,\n  })\n\n  return state","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/runtime/utils.ts#L3-L39","documentation":"Thrown by getWorkerState when globalThis.__vitest_worker__ is unset. That global is injected only into the worker/context that Vitest's own runtime spawns; the error means code is running somewhere Vitest never initialized. The message lists the four common causes and the issue tracker, because nearly all reports are usage mistakes rather than bugs.","triggerScenarios":"Importing `vitest` (or anything that calls getWorkerState, e.g. vi, expect from vitest) from: a plain Node script run with `node`; a globalSetup file (which runs in the main process, not a test worker); the Vite/Vitest config file itself; or a context Vitest did not instrument.","commonSituations":"Putting `import { vi } from 'vitest'` in a globalSetup that sets up test databases; importing vitest inside vitest.config.ts to read config; running a test file directly with `node test.ts` instead of `vitest`; a third-party tool that imports vitest at module-eval time outside a worker.","solutions":["Move code that imports vitest from `globalSetup` into `setupFiles` (setupFiles run inside the worker where the state exists).","Do not import `vitest` from your Vite/Vitest config file; read config from the passed-in `defineConfig` argument instead.","Run tests via the `vitest` CLI (or the vitest node API), never by executing test files with `node`/`tsx` directly.","If you truly need worker state in a non-worker context, this is unsupported — restructure so the code runs inside a test or setup file."],"exampleFix":"// before — vitest.config.ts globalSetup imports vitest\nimport { vi } from 'vitest'\nexport default defineConfig({\n  globalSetup: ['./global-setup.ts'], // global-setup.ts does `import { vi } from 'vitest'`\n})\n\n// after — move the logic into setupFiles\nexport default defineConfig({\n  setupFiles: ['./setup.ts'], // runs inside the worker; `import { vi } from 'vitest'` is valid here\n})","handlingStrategy":"try-catch","validationCode":"// Detect you are inside a Vitest worker before importing vitest\nfunction inVitestWorker(): boolean {\n  return !!(globalThis as any).__vitest_worker__\n}\nif (!inVitestWorker()) {\n  // do not import 'vitest' here\n}","typeGuard":"function hasWorkerState(g: typeof globalThis): boolean {\n  return !!(g as any).__vitest_worker__\n}","tryCatchPattern":"try {\n  const { vi } = await import('vitest')\n} catch (e) {\n  if (String(e?.message).startsWith('Vitest failed to access its internal state')) {\n    // you imported vitest outside a worker; move this code into setupFiles\n    console.error('Move vitest imports into setupFiles, not globalSetup or config')\n  }\n  throw e\n}","preventionTips":["Never import vitest inside vitest.config.ts or globalSetup files.","Run tests only through the vitest CLI or its node API.","If a dependency imports vitest at module-eval time, mock or restructure it."],"tags":["runtime","worker-state","config","globalsetup"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}