{"id":"190b1c4f631c0c3c","repo":"vitest-dev/vitest","slug":"worker-state-is-not-found-this-is-an-issue-with-v","errorCode":null,"errorMessage":"Worker state is not found. This is an issue with Vitest. Please, open an issue.","messagePattern":"Worker state is not found\\. This is an issue with Vitest\\. Please, open an issue\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"packages/browser/src/client/utils.ts","lineNumber":125,"sourceCode":"    off: (event: string, listener: (payload: any) => void) => void\n    send: (method: string, params?: Record<string, unknown>) => Promise<unknown>\n    emit: (event: string, payload: unknown) => void\n  }\n  aria: typeof import('ivya/aria')\n}\n\n/* @__NO_SIDE_EFFECTS__ */\nexport function getBrowserState(): BrowserRunnerState {\n  // @ts-expect-error not typed global\n  return window.__vitest_browser_runner__\n}\n\n/* @__NO_SIDE_EFFECTS__ */\nexport function getWorkerState(): WorkerGlobalState {\n  // @ts-expect-error not typed global\n  const state = window.__vitest_worker__\n  if (!state) {\n    throw new Error('Worker state is not found. This is an issue with Vitest. Please, open an issue.')\n  }\n  return state\n}\n","sourceCodeStart":107,"sourceCodeEnd":129,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/browser/src/client/utils.ts#L107-L129","documentation":"Thrown by `getWorkerState` at utils.ts:124-126 when `window.__vitest_worker__` is unset. The browser tester sets this global in state.ts:52 during boot; if it's missing, the code is running in a context that did not load the tester bootstrap (e.g. the top-level UI window, a popup, or before injection completed).","triggerScenarios":"Calling `getWorkerState()` from the Vitest UI top frame, from a script loaded before the tester bootstrap, or from a context where the injection at esm-client-injector.js failed. Also from a manually opened browser tab pointed at the tester URL.","commonSituations":"Custom scripts loaded into the browser that aren't part of the test runtime. Misconfigured injection (e.g. `globalThisAccessor` tampering). Opening the iframe URL in a new tab to 'debug'.","solutions":["Only call worker-state-dependent APIs from within the tester iframe loaded by Vitest.","Verify the tester entry was loaded: `if (window.__vitest_worker__) { ... }` before calling.","Restart the browser runner; if the bootstrap injection fails it's a Vitest/setup issue."],"exampleFix":"// before\nconst state = getWorkerState() // throws in non-tester context\n\n// after\nimport { getBrowserState } from '@vitest/browser/client'\nif (getBrowserState() && (window as any).__vitest_worker__) {\n  const state = getWorkerState()\n}","handlingStrategy":"validation","validationCode":"if (!(window as any).__vitest_worker__) {\n  throw new Error('not running inside the Vitest tester iframe')\n}","typeGuard":"function hasWorkerState(w: Window = window): boolean {\n  return !!(w as any).__vitest_worker__\n}","tryCatchPattern":null,"preventionTips":["Only call worker-state APIs from the tester iframe runtime.","Guard with window.__vitest_worker__ before invoking.","Restart the browser runner if injection appears to have failed."],"tags":["browser","worker-state","lifecycle","vitest-bug","internal-api"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}