-
Notifications
You must be signed in to change notification settings - Fork 119
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
Mock ws inside web worker #356
Comments
What library do you use for testing workers? I can suggest creating another module for WebSocket (and import it from the worker) and trying to mock this module using jest or sinon if it works for you |
I am running the component in storybook and I do not mock workers.
So I wonder if there is a way around this problem. I tried injecting WebSocket implementation from Window object to Worker but had no luck |
Anyway, it's not enough to mock the WebSocket, you need all the code inside the worker scope. So you need to have also a Server instance from this module and describe how it should work. So you can't manage it outside this worker. Just to note: when you create a Server instance the WebSocket is mocked automatically and is restored when you call You can try to inject some script using But I'd suggest changing the code a little bit to have the WebSocket in the main thread, so instead of sending and receiving the data to WebSocket it would send and receive events to the main thread. It'd have all the logic inside the worker, and it'd be possible to manage the WebSocket in the main thread, so you can mock it the same way:
|
Currently mocking web sockets within web worker is not possible.
Window and global object within web worker is not shared
The text was updated successfully, but these errors were encountered: