Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.34.2 breaks web workers when not using jsdom #3974

Closed
6 tasks done
marvinruder opened this issue Aug 17, 2023 · 1 comment · Fixed by #4014
Closed
6 tasks done

v0.34.2 breaks web workers when not using jsdom #3974

marvinruder opened this issue Aug 17, 2023 · 1 comment · Fixed by #4014

Comments

@marvinruder
Copy link

Describe the bug

Given that vitest is run without an environment like jsdom (i.e. the test.environment config attribute is unset), when importing a worker using the syntax import MyWorker from '../src/worker?worker' and creating an instance of this worker using new MyWorker(), then tests fail with the following stack trace:

 FAIL  test/init.test.ts > simple worker
ReferenceError: self is not defined
 ❯ getFileIdFromUrl ../../packages/web-worker/dist/pure.js:347:24
 ❯ new Worker ../../packages/web-worker/dist/pure.js:408:18
 ❯ new WorkerWrapper src/worker.ts:2:18
      1| self.onmessage = (e) => {
      2|   self.postMessage(`${e.data} world`)
       |                  ^
      3| }
      4| 
 ❯ test/init.test.ts:37:20

Note that @vitest/web-workers explicitly states in their README that jsdom is not required:

> Web Worker support for Vitest testing. Doesn't require JSDom.

Reproduction

https://github.com/marvinruder/vitest

  1. Open Repository in GitHub Codespace
  2. Run pnpm install --frozen-lockfile
  3. Run pnpm build
  4. Run cd test/web-worker
  5. Run pnpm test and observe that many tests fail, e.g. test/init.test.ts > simple worker

System Info

System:
    OS: Linux 5.15 Ubuntu 20.04.6 LTS (Focal Fossa)
    CPU: (2) x64 Intel(R) Xeon(R) Platinum 8272CL CPU @ 2.60GHz
    Memory: 2.21 GB / 3.82 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 20.5.0 - ~/nvm/current/bin/node
    Yarn: 1.22.19 - /usr/bin/yarn
    npm: 9.8.0 - ~/nvm/current/bin/npm
    pnpm: 8.6.6 - /workspaces/vitest/node_modules/.bin/pnpm
  npmPackages:
    @vitest/browser: workspace:* => 0.34.2 
    @vitest/coverage-istanbul: workspace:* => 0.34.2 
    @vitest/coverage-v8: workspace:* => 0.34.2 
    @vitest/ui: workspace:* => 0.34.2 
    vite: ^4.2.1 => 4.3.9 
    vitest: workspace:* => 0.34.2

Used Package Manager

pnpm

Validations

@marvinruder
Copy link
Author

Introduced in #3950

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants