Skip to content

Dynamic Imports within web workers are broken (browser mode) #6552

@alexlwn123

Description

@alexlwn123

Describe the bug

globalThis.__vitest_broser_runner__ is not accessible within the scope of web workers, so vite's dynamic import wrapping is breaking via:
image

Adding this hack to the top of the worker seems to fix the issue, but it's still a bug nonetheless:

image

Reproduction

I'm struggling to repro on StackBlitz due to browser mode incompatibilities on the web-based IDE.

Here's some links to my open source project where this applies:

To reproduce, start a web worker that contains a dynamic import...

// main.ts
const worker = new Worker(new URL('./worker-file.js', import.meta.url), { type: 'module' })

// ...

// worker-file.js

const asset = await import('any-package-or-file')

System Info

System:
    OS: macOS 14.6.1
    CPU: (10) x64 Apple M1 Max
    Memory: 24.88 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.17.0 - ~/.local/state/fnm_multishells/19453_1726068848957/bin/node
    Yarn: 1.22.21 - /usr/local/bin/yarn
    npm: 10.8.2 - ~/.local/state/fnm_multishells/19453_1726068848957/bin/npm
    pnpm: 9.4.0 - /usr/local/bin/pnpm
    bun: 1.1.26 - ~/.bun/bin/bun
    Watchman: 2024.05.06.00 - /usr/local/bin/watchman
  Browsers:
    Brave Browser: 128.1.69.168
    Safari: 17.6
  npmPackages:
    @vitest/browser: ^2.1.1 => 2.1.1 
    @vitest/coverage-v8: ^2.1.1 => 2.1.1 
    @vitest/ui: ^2.1.1 => 2.1.1 
    vitest: ^2.1.1 => 2.1.1

Used Package Manager

pnpm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: browserIssues and PRs related to the browser runnerp3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions