jest-websocket-mock icon indicating copy to clipboard operation
jest-websocket-mock copied to clipboard

Does not work with React <= 16.8.6

Open mehagar opened this issue 3 years ago • 1 comments

I'm trying to get this example from the README to work:

import WS from 'jest-websocket-mock';

it('tests websocket', 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"]);
});

When I run that, I get this error:

console.error
    Warning: The callback passed to ReactTestUtils.act(...) function must not return anything.
    
    It looks like you wrote ReactTestUtils.act(async () => ...), or returned a Promise from the callback passed to it. Putting asynchronous logic inside ReactTestUtils.act(...) is not supported.
    

       5 |   const client = new WebSocket("ws://localhost:1234");
       6 |
    >  7 |   await server.connected;
         |                ^
       8 |   client.send("hello");
       9 |   await expect(server).toReceiveMessage("hello");
      10 |   expect(server).toHaveReceivedMessages(["hello"]);

      at warningWithoutStack (node_modules/react-dom/cjs/react-dom-test-utils.development.js:100:32)
      at act (node_modules/react-dom/cjs/react-dom-test-utils.development.js:1172:9)
      at waitForConnected (node_modules/jest-websocket-mock/lib/jest-websocket-mock.cjs.js:128:13)
      at WS.connected (node_modules/jest-websocket-mock/lib/jest-websocket-mock.cjs.js:134:5)

And the test fails because it exceeded the 5 second timeout for a test. How can I get this to work? I'm using version 2.3.0 of jest-websocket-mock and Node 14.17.0.

mehagar avatar Feb 17 '22 19:02 mehagar

Looks like the issue was my React version. I was using version 16.8.6. Upgrading to 16.9.0 worked. Could this be specified in the docs, maybe as a NPM peerDependency?

mehagar avatar Feb 17 '22 19:02 mehagar