vitest-dev/vitest · error · Error

Not called in the browser

Error message

Not called in the browser

What it means

Thrown by the `environment.setup` stub at state.ts:32-34 inside the browser-side `WorkerGlobalState`. In the browser pool the environment is already 'browser' and the setup function should never be invoked; calling it indicates code that assumed a Node-style environment worker lifecycle. It's a guard against misuse of the synthetic worker state.

Source

Thrown at packages/browser/src/client/tester/state.ts:33

    config,
    projectName: config.name || '',
    metaEnv: null as any,
    files: [],
    environment: {
      name: 'browser',
      options: null,
    },
    // this is populated before tests run
    providedContext: {},
    invalidates: [],
  },
  onCancel: null as any,
  config,
  environment: {
    name: 'browser',
    viteEnvironment: 'client',
    setup() {
      throw new Error('Not called in the browser')
    },
  },
  onCleanup: fn => getBrowserState().cleanups.push(fn),
  evaluatedModules: new EvaluatedModules(),
  resolvingModules: new Set(),
  moduleExecutionInfo: new Map(),
  metaEnv: null as any,
  rpc: null as any,
  durations: {
    environment: 0,
    prepare: performance.now(),
  },
  providedContext: {},
}

// @ts-expect-error not typed global
globalThis.__vitest_browser__ = true
// @ts-expect-error not typed global

View on GitHub (pinned to d568f8ce37)

Solutions

  1. Do not call `environment.setup()` in browser tests; the browser environment is already active.
  2. If sharing code between Node and browser pools, guard the call: `if (state.environment.name !== 'browser') state.environment.setup()`.
  3. Move setup logic into `setupFiles` referenced by config instead of invoking the environment API directly.

Example fix

// before
getWorkerState().environment.setup()

// after (browser-safe)
if (getWorkerState().environment.name !== 'browser') {
  getWorkerState().environment.setup()
}
Defensive patterns

Strategy: validation

Validate before calling

import { getWorkerState } from '@vitest/browser/client'
const state = getWorkerState()
if (state.environment.name !== 'browser') {
  state.environment.setup()
}

Prevention

When it happens

Trigger: Calling `getWorkerState().environment.setup()` from test code or a plugin running in the browser iframe. Library code (e.g. a custom environment plugin) that calls `environment.setup()` generically across all workers.

Common situations: Importing a Node-targeted environment adapter into a browser test. A plugin/hook that enumerates worker environments and calls `setup()` unconditionally.

Related errors


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