-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Worker is not defined #3449
Comments
You'll need to create your own mock for web workers. This is an unsupported feature and should either be part of your own mocks or part of jsdom, but not Jest. Feel free to make a mock and put it on npm for other people to use as well :) |
Thanks @cpojer! Wasn't entirely sure. Thanks again for the advice. :) |
Sorry to comment on a closed issue, but I'm having the same problem, and can't find another way of contacting you @patoncrispy... |
Hey @israelss! I just extracted as much of my code from the worker into a separate module and tested that. I want to write my own mocks for a web worker, but a bit busy at the moment. |
Thanks for reply @patoncrispy, and sorry for late response! |
setting window.Worker = someClass should do the trick |
If anyone else is looking for a Worker stub here is one I made. I haven't had any issues with it yet.
If you are using create-react-app you can stub workers globally by adding this to src/setupTests.js. |
for anyone using ESM and creating worker separately in a js file (and use
and in your class Worker {
constructor(stringUrl) {
this.url = stringUrl;
this.onmessage = () => {};
}
postMessage(msg) {
this.onmessage(msg);
}
}
export default Worker; and don't forget to call spent a lot time struggling with this! |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Do you want to request a feature or report a bug?
A bug.
What is the current behavior?
When trying to test a web worker, the Worker constructor is throwing an error saying it is undefined.
Here is an example: https://repl.it/HdhL/0
What is the expected behavior?
The Worker constructor creates a new Web Worker.
Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.
Jest Config:
Jest Version: 19.0.2
Yarn: 0.23.2
Windows 10 (w/ Creators Update)
The text was updated successfully, but these errors were encountered: