Worker is not defined

See original GitHub issue

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": {
    "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:closed
  • Created 6 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

22reactions
djb5143commented, Nov 27, 2017

If anyone else is looking for a Worker stub here is one I made. I haven’t had any issues with it yet.

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

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

If you are using create-react-app you can stub workers globally by adding this to src/setupTests.js. window.Worker = Worker;

5reactions
kamaladenalhomsicommented, Dec 29, 2020

for anyone using ESM and creating worker separately in a js file (and use 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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found