{"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":"error","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/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/src/runtime/utils.ts#L3-L39","documentation":"Thrown by getWorkerState when the global `__vitest_worker__` symbol is missing from globalThis. That symbol is injected by Vitest's runner into each worker/context. When it is absent, it almost always means `vitest` (or code that calls getWorkerState, like expect/vi) is being imported and executed outside of a Vitest worker - e.g. directly by node, inside globalSetup, or inside the config file itself.","triggerScenarios":"Running a file with `node my-test.js` that does `import {describe} from 'vitest'`. Importing `vitest` inside a globalSetup file. Importing `vitest`/`@vitest/expect`/`vi` inside the vitest.config.ts or vite.config.ts. Calling vi.mock or expect in a context Vitest did not initialize.","commonSituations":"Trying to reuse Vitest's API in a plain Node script. Putting shared helpers that import vitest into globalSetup. Tooling (eslint plugins, typegen) that imports the config file and accidentally evaluates a vitest import.","solutions":["Run tests via the `vitest` CLI instead of `node`/`ts-node`.","Move code that imports `vitest` out of `globalSetup` into `setupFiles` (globalSetup runs in the main process, not a worker).","Do not import `vitest`, `vi`, or `@vitest/expect` inside your vite/vitest config file.","If this fires during normal `vitest` runs, it may be a Vitest bug - report it with a reproduction."],"exampleFix":"// before (vitest.config.ts) - throws\nimport { vi } from 'vitest'\nexport default defineConfig({ test: { globalSetup: ['./setup.ts'] } })\n\n// after\n// vitest.config.ts - do not import vitest here\nimport { defineConfig } from 'vitest/config'\nexport default defineConfig({ test: { setupFiles: ['./setup.ts'] } })\n// move any `import { ... } from 'vitest'` into ./setup.ts or test files","handlingStrategy":"validation","validationCode":"// Only call vitest APIs when running under Vitest\nfunction assertVitestContext() {\n  if (!globalThis.__vitest_worker__) {\n    throw new Error('vitest APIs called outside a Vitest worker')\n  }\n}","typeGuard":"function isVitestWorker(): boolean {\n  return Boolean((globalThis as any).__vitest_worker__)\n}","tryCatchPattern":null,"preventionTips":["Run tests via the vitest CLI only.","Never import vitest/vi/expect in globalSetup or the config file.","Move such imports into setupFiles or test files."],"tags":["runtime","config","worker-state"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}