romgain / jest-websocket-mock

Mock websockets and assert complex websocket interactions with Jest
MIT License
175 stars 28 forks source link

Timeout error running simple test in jest #167

Open ravi-2912 opened 9 months ago

ravi-2912 commented 9 months ago

I am running the following test in NodeJS.

import WS from 'jest-websocket-mock';
import WebSocket from 'ws';

test('example test with jest-websocket-mock', async () => {
    const server = new WS('ws://localhost:1234');
    const client = new WebSocket('ws://localhost:1234');

    await server.connected;
    client.send('hello');
    await expect(server).toReceiveMessage('hello');
    expect(server).toHaveReceivedMessages(['hello']);
}, 25000);

And I get the following error

image

Please could you suggest how can I resolve this.

EsmaNErdem commented 7 months ago

I have the same issue where I am trying mock server side websocket to test React client side connection. Were you able to solve this?

Sheldenshi commented 4 months ago

Having the same issue

Sheldenshi commented 4 months ago

You need to import WebSocket from 'mock-socket'

import { WebSocket } from 'mock-socket';