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

Worker is not defined #3449

Closed
patoncrispy opened this issue May 3, 2017 · 9 comments
Closed

Worker is not defined #3449

patoncrispy opened this issue May 3, 2017 · 9 comments

Comments

@patoncrispy
Copy link

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)

@cpojer
Copy link
Member

cpojer commented May 3, 2017

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 :)

@cpojer cpojer closed this as completed May 3, 2017
@patoncrispy
Copy link
Author

Thanks @cpojer! Wasn't entirely sure. Thanks again for the advice. :)

@israelss
Copy link

Sorry to comment on a closed issue, but I'm having the same problem, and can't find another way of contacting you @patoncrispy...
Did you managed to work this out? Feel free to answer me by email!

@patoncrispy
Copy link
Author

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.

@israelss
Copy link

Thanks for reply @patoncrispy, and sorry for late response!
I was able to mock the module that uses window.Worker, but not the Worker itself. I'll leave it that way for now.
Thanks again!

@belohlavek
Copy link

setting window.Worker = someClass should do the trick

@djb5143
Copy link

djb5143 commented 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;

@kamaladenalhomsi
Copy link

kamaladenalhomsi commented Dec 23, 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!

@github-actions
Copy link

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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants