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

Web Workers in Jest #214

Closed
thkruz opened this issue Dec 29, 2020 · 1 comment
Closed

Web Workers in Jest #214

thkruz opened this issue Dec 29, 2020 · 1 comment

Comments

@thkruz
Copy link
Owner

thkruz commented Dec 29, 2020

for anyone using ESM and creating worker separately in a js file (and using worker-loader to load the worker)
setting window.Worker = Worker is not going to work since here we are importing the worker as a module.
create __mocks__ folder and do a manual mock for your worker module, e.g:

├── src
│   ├── __mocks__
│   │   └── myworker.worker..js
│   └── myworker.worker..js

and in your __mocks__/myworker.worker.js file:

class Worker {
  constructor(stringUrl) {
    this.url = stringUrl;
    this.onmessage = () => {};
  }

  postMessage(msg) {
    this.onmessage(msg);
  }
}

export default Worker;

and don't forget to call jest.mock('myworker.worker.js')

spent a lot time struggling with this!

Originally posted by @kamaladenalhomsi in jestjs/jest#3449 (comment)

@thkruz
Copy link
Owner Author

thkruz commented Apr 29, 2021

Fixed in ca2f97e

@thkruz thkruz closed this as completed Apr 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant