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 socket.io-client? window.io is not populated #301

Open
erezcohen opened this issue Jun 20, 2020 · 2 comments
Open

How to mock socket.io-client? window.io is not populated #301

erezcohen opened this issue Jun 20, 2020 · 2 comments

Comments

@erezcohen
Copy link

Hi Guys, I'm trying to test a react app which uses socket.io-client (with cypress).
I saw that the recommended approach is to set window.io = SocketIO however it seems like socket.io-client does not use this global var.
I saw that in #169 you recommended setting the mock in the production code however I wanted to know is it possible to mock without touching this code.
Thanks

@angelo-hub
Copy link

angelo-hub commented May 26, 2023

if using jest you can do

import { Server, SocketIO as mockSocketIO } from 'mock-socket';

export const server = new Server(URL_HERE);

jest.mock('socket.io-client', () => mockSocketIO);

@MonkeyDo
Copy link

MonkeyDo commented Nov 20, 2023

Building on @angelo-hub's response (thanks!), I had to do the following:

import { SocketIO as mockSocketIO } from "mock-socket";

jest.mock("socket.io-client", () => ({
  ...mockSocketIO,
  io: mockSocketIO.connect,
}));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants