Worker is not defined
See original GitHub issueDo 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": {
"globals": {
"window": true
},
"testResultsProcessor": "./node_modules/jest-junit",
"moduleFileExtensions": [
"js"
],
"moduleDirectories": [
"node_modules"
],
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
"\\.(css|scss|html)$": "identity-obj-proxy"
}
}
Jest Version: 19.0.2 Yarn: 0.23.2 Windows 10 (w/ Creators Update)
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (1 by maintainers)
Top Results From Across the Web
Worker is not defined for target Node.js #12299 - GitHub
Bug report I am trying to use the new Webpack 5 native worker features. per ... Worker is not defined for target Node.js...
Read more >Uncaught ReferenceError: Worker is not defined while trying ...
var worker = new Worker('foo.js');. I get an exception in Chrome. Uncaught ReferenceError: Worker is not defined.
Read more >Worker() - Web APIs - MDN Web Docs
The Worker() constructor creates a Worker object that executes the script at the specified URL. This script must obey the same-origin policy.
Read more >Worker threads | Node.js v19.3.0 Documentation
Receive a single message from a given MessagePort . If no message is available, undefined is returned, otherwise an object with a single...
Read more >#5 - Does not work in web worker - atob.js - CoolAJ86 on GIT
Is atob defined on... self or whatever it is that workers have? Does globalThis work?
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
window.Worker = Worker;for anyone using ESM and creating worker separately in a js file (and use
worker-loaderto load the worker) settingwindow.Worker = Workeris 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:and in your
__mocks__/myworker.worker.jsfile:and don’t forget to call
jest.mock('myworker.worker.js')spent a lot time struggling with this!