{"id":"ecc623426280a122","repo":"vitest-dev/vitest","slug":"not-called-in-the-browser","errorCode":null,"errorMessage":"Not called in the browser","messagePattern":"Not called in the browser","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/browser/src/client/tester/state.ts","lineNumber":33,"sourceCode":"    config,\n    projectName: config.name || '',\n    metaEnv: null as any,\n    files: [],\n    environment: {\n      name: 'browser',\n      options: null,\n    },\n    // this is populated before tests run\n    providedContext: {},\n    invalidates: [],\n  },\n  onCancel: null as any,\n  config,\n  environment: {\n    name: 'browser',\n    viteEnvironment: 'client',\n    setup() {\n      throw new Error('Not called in the browser')\n    },\n  },\n  onCleanup: fn => getBrowserState().cleanups.push(fn),\n  evaluatedModules: new EvaluatedModules(),\n  resolvingModules: new Set(),\n  moduleExecutionInfo: new Map(),\n  metaEnv: null as any,\n  rpc: null as any,\n  durations: {\n    environment: 0,\n    prepare: performance.now(),\n  },\n  providedContext: {},\n}\n\n// @ts-expect-error not typed global\nglobalThis.__vitest_browser__ = true\n// @ts-expect-error not typed global","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/browser/src/client/tester/state.ts#L15-L51","documentation":"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.","triggerScenarios":"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.","commonSituations":"Importing a Node-targeted environment adapter into a browser test. A plugin/hook that enumerates worker environments and calls `setup()` unconditionally.","solutions":["Do not call `environment.setup()` in browser tests; the browser environment is already active.","If sharing code between Node and browser pools, guard the call: `if (state.environment.name !== 'browser') state.environment.setup()`.","Move setup logic into `setupFiles` referenced by config instead of invoking the environment API directly."],"exampleFix":"// before\ngetWorkerState().environment.setup()\n\n// after (browser-safe)\nif (getWorkerState().environment.name !== 'browser') {\n  getWorkerState().environment.setup()\n}","handlingStrategy":"validation","validationCode":"import { getWorkerState } from '@vitest/browser/client'\nconst state = getWorkerState()\nif (state.environment.name !== 'browser') {\n  state.environment.setup()\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never call environment.setup() inside browser tests.","Guard any environment-agnostic helper with a name check.","Put environment bootstrap in setupFiles rather than manual calls."],"tags":["browser","worker-state","environment","lifecycle","internal-api"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}