Skip to content

Cannot access 'browser' before initialization #7784

@Stadly

Description

@Stadly

Describe the bug

I get a Cannot access 'browser' before initialization error when running Vitest Browser Mode. The error came after updating to Vitest 3.1.1

13:52:14 [vite] (ssr) Error when evaluating SSR module /node_modules/@sveltejs/kit/src/runtime/control.js: Cannot access 'browser' before initialization
  Plugin: vitest:mocks
  File: /home/projects/sveltejs-kit-template-default-lck3k5rf/node_modules/@sveltejs/kit/src/runtime/control.js
      at Object.filter (file:///home/projects/sveltejs-kit-template-default-lck3k5rf/node_modules/vitest/dist/chunks/cli-api.bwYuoT4p.js:10506:45)
      at filter (file:///home/projects/sveltejs-kit-template-default-lck3k5rf/node_modules/vitest/dist/chunks/cli-api.bwYuoT4p.js:9085:20)
      at TransformPluginContext.transform (file:///home/projects/sveltejs-kit-template-default-lck3k5rf/node_modules/@vitest/mocker/dist/node.js:722:9)
      at EnvironmentPluginContainer.transform (file:///home/projects/sveltejs-kit-template-default-lck3k5rf/node_modules/vite/dist/node/chunks/dep-DrOo5SEf.js:47726:19)
      at async loadAndTransform (file:///home/projects/sveltejs-kit-template-default-lck3k5rf/node_modules/vite/dist/node/chunks/dep-DrOo5SEf.js:41383:27)

Reproduction

https://stackblitz.com/edit/vitest-cannot-access-browser?file=vitest.workspace.ts

Created like this:

  1. New StackBlitz from SvelteKit template
  2. npx sv add and select vitest
  3. npx init vitest browser:
    ✔ Choose a language for your tests › TypeScript
    ✔ Choose a browser provider. Vitest will use its API to control the testing environment › playwright
    ✔ Choose a browser › chromium
    ✔ Choose your framework › svelte
    ✔ Install Playwright browsers (can be done manually via 'pnpm exec playwright install')? … yes
  4. npm run vitest:browser

I get the same error locally in my Dev Container, so I don't think it is due to issues with Playwright and StackBlitz.

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.20.3 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    @vitest/browser: ^3.1.1 => 3.1.1 
    playwright: ^1.51.1 => 1.51.1 
    vite: ^6.0.0 => 6.2.4 
    vitest: ^3.0.0 => 3.1.1 
    vitest-browser-svelte: ^0.1.0 => 0.1.0

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions