vitest-dev/vitest · error · Error

Not called in the browser

Error message

Not called in the browser

What it means

Thrown by the `setup()` method of the `environment` object on the browser-side `WorkerGlobalState`. This state object is constructed only inside the browser tester; its `environment.setup` is a stub because real environment setup happens server-side before the browser context is loaded. Calling it indicates the browser worker state leaked into a non-browser caller (e.g. imported in Node) or code incorrectly invoked `environment.setup()`.

Solutions

  1. Do not call `environment.setup()` on the browser worker state; it is set up by the runner infrastructure.
  2. Ensure setup files and custom environments run in the correct environment (Node vs `browser`).
  3. If this surfaces unexpectedly, it is likely a Vitest internal bug — open an issue with a reproduction.
Defensive patterns

Strategy: validation

Validate before calling

// do not call state.environment.setup(); ensure you are not in Node when using browser state
if (typeof window === 'undefined') {
  throw new Error('Browser worker state is only valid in the browser tester')
}

Prevention

When it happens

Trigger: Code that imports the browser tester state module in Node and calls `state.environment.setup()`; a setup file executed in the wrong environment; a misuse of internal Vitest APIs that reaches this stub.

Common situations: Mixing Node-side and browser-side Vitest internals; a custom environment/runner that calls `environment.setup()` on the browser state object; importing `vitest/browser` internals in a Node test file.

Related errors


AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11). Data as JSON: /api/errors/ecc623426280a122. Report an issue: GitHub.

Appendix: 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(),
    fetch: 0,
  },
  providedContext: {},
}

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

View on GitHub (pinned to 1fa9837ec2)