vitest-dev/vitest · critical · Error

Worker state is not found. This is an issue with Vitest. Ple

Error message

Worker state is not found. This is an issue with Vitest. Please, open an issue.

What it means

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).

Source

Thrown at packages/browser/src/client/utils.ts:125

    off: (event: string, listener: (payload: any) => void) => void
    send: (method: string, params?: Record<string, unknown>) => Promise<unknown>
    emit: (event: string, payload: unknown) => void
  }
  aria: typeof import('ivya/aria')
}

/* @__NO_SIDE_EFFECTS__ */
export function getBrowserState(): BrowserRunnerState {
  // @ts-expect-error not typed global
  return window.__vitest_browser_runner__
}

/* @__NO_SIDE_EFFECTS__ */
export function getWorkerState(): WorkerGlobalState {
  // @ts-expect-error not typed global
  const state = window.__vitest_worker__
  if (!state) {
    throw new Error('Worker state is not found. This is an issue with Vitest. Please, open an issue.')
  }
  return state
}

View on GitHub (pinned to d568f8ce37)

Solutions

  1. Only call worker-state-dependent APIs from within the tester iframe loaded by Vitest.
  2. Verify the tester entry was loaded: `if (window.__vitest_worker__) { ... }` before calling.
  3. Restart the browser runner; if the bootstrap injection fails it's a Vitest/setup issue.

Example fix

// before
const state = getWorkerState() // throws in non-tester context

// after
import { getBrowserState } from '@vitest/browser/client'
if (getBrowserState() && (window as any).__vitest_worker__) {
  const state = getWorkerState()
}
Defensive patterns

Strategy: validation

Validate before calling

if (!(window as any).__vitest_worker__) {
  throw new Error('not running inside the Vitest tester iframe')
}

Type guard

function hasWorkerState(w: Window = window): boolean {
  return !!(w as any).__vitest_worker__
}

Prevention

When it happens

Trigger: 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.

Common situations: 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'.

Related errors


AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03). Data as JSON: /data/errors/190b1c4f631c0c3c.json. Report an issue: GitHub.