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

How to mock a worker when testing with jest? Should we even test workers? #11

Open
manjula-dube opened this issue Feb 10, 2018 · 10 comments
Labels
good first issue Good for newcomers question Further information is requested

Comments

@manjula-dube
Copy link
Collaborator

ReferenceError: Worker is not defined

@manjula-dube manjula-dube added the question Further information is requested label Feb 10, 2018
@manjula-dube
Copy link
Collaborator Author

Reference: jestjs/jest#3449

@manjula-dube manjula-dube changed the title How to mock a worker? Should we even test workers? How to mock a worker when testing with jest? Should we even test workers? Feb 10, 2018
@nitish24p
Copy link
Owner

wondering if setting a mock worker class would work

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

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

and then doing

window.Worker = Worker

@manjula-dube
Copy link
Collaborator Author

I tried but it doesn't work I am wondering should we even test external resources ?

@prateekbh
Copy link

did you guys get to close this?
i am kinda stuck on the same thing

@hayait599
Copy link

Same issue :(

@nitish24p
Copy link
Owner

Havent had time to see this but maybe this would help ? https://www.npmjs.com/package/jsdom-worker

@andreasjhagen
Copy link

Havent had time to see this but maybe this would help ? https://www.npmjs.com/package/jsdom-worker

Thanks, the package actually works. It produces some error message though, even when all tests pass. It doesn't look like it's maintained any longer unfortunately

@andreasjhagen
Copy link

For people like me, that want to keep the console clean and want to get rid of the error message after successfull tests, I created a fork and fixed it:

https://www.npmjs.com/package/jsdom-worker-fix

@ghost
Copy link

ghost commented May 12, 2020

For people like me, that want to keep the console clean and want to get rid of the error message after successfull tests, I created a fork and fixed it:

https://www.npmjs.com/package/jsdom-worker-fix

@andreasjhagen can u show implementation in create-react-app, if possible

@manjula-dube manjula-dube added the good first issue Good for newcomers label May 16, 2020
@ViniciusResende
Copy link

ViniciusResende commented Jul 6, 2021

If your working with Shared Workers this is what has worked for me.
First, you create a mock folder like this :

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

And then you can add a mock for your worker like this:

class SharedWorker {
  constructor(stringUrl) {
    this.url = stringUrl;
    this.onconnect = () => {};
    this.port = {
      start: () => {},
      postMessage: () => {},
      onmessage: () => {},
    };
  }
}

export default SharedWorker;

finally, in your tests that require your worker, you can do this:

jest.mock('./path-to-your-worker')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers question Further information is requested
Projects
None yet
Development

No branches or pull requests

6 participants