question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Upgrading from 4.1.3 to 4.4.0 breaks my Jest unit tests

See original GitHub issue

Describe the bug I’ve updated Socket.IO in our setup from v. 4.1.3 to v. 4.4.0 and our custom socket tests began failing. I tried the basic example provided here, and after the upgrade it fails too .

To Reproduce Install Socket.IO to v. 4.1.3, and example tests pass. Install Socket.IO to v. 4.4.0, and example tests fails.

backend-socket-validator-2.spec.ts

const { createServer } = require("http");
const { Server } = require("socket.io");
const Client = require("socket.io-client");

describe("my awesome project", () => {
  let io, serverSocket, clientSocket;

  beforeAll((done) => {
    const httpServer = createServer();
    io = new Server(httpServer);
    httpServer.listen(() => {
      const port = httpServer.address().port;
      clientSocket = new Client(`http://localhost:${port}`);
      io.on("connection", (socket) => {
        serverSocket = socket;
      });
      clientSocket.on("connect", done);
    });
  });

  afterAll(() => {
    io.close();
    clientSocket.close();
  });

  test("should work", (done) => {
    clientSocket.on("hello", (arg) => {
      expect(arg).toBe("world");
      done();
    });
    serverSocket.emit("hello", "world");
  });

  test("should work (with ack)", (done) => {
    serverSocket.on("hi", (cb) => {
      cb("hola");
    });
    clientSocket.emit("hi", (arg) => {
      expect(arg).toBe("hola");
      done();
    });
  });
});

My output running v. 4.1.3

PASS backend-authentication libs/backend/authentication/src/lib/socket-validator/backend-socket-validator-2.spec.ts my awesome project √ should work (7 ms) √ should work (with ack) (2 ms)

Test Suites: 1 passed, 1 total Tests: 2 passed, 2 total Snapshots: 0 total Time: 3.161 s Ran all test suites matching /c:\Users\<WORKSPACE>backend-socket-validator-2.spec.ts/i with tests matching “my awesome project”.

My output running v. 4.4.0 ● my awesome project › should work

thrown: "Exceeded timeout of 5000 ms for a hook.
Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."

   6 |     let io, serverSocket, clientSocket;
   7 |
>  8 |     beforeAll((done) => {
     |     ^
   9 |         const httpServer = createServer();
  10 |         io = new Server(httpServer);
  11 |         httpServer.listen(() => {

  at src/lib/socket-validator/backend-socket-validator-2.spec.ts:8:5
  at Object.<anonymous> (src/lib/socket-validator/backend-socket-validator-2.spec.ts:5:1)
  at TestScheduler.scheduleTests (../../../node_modules/@jest/core/build/TestScheduler.js:333:13)
  at runJest (../../../node_modules/@jest/core/build/runJest.js:401:19)
  at _run10000 (../../../node_modules/@jest/core/build/cli/index.js:320:7)
  at runCLI (../../../node_modules/@jest/core/build/cli/index.js:173:3)

● my awesome project › should work (with ack)

thrown: "Exceeded timeout of 5000 ms for a hook.
Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."

   6 |     let io, serverSocket, clientSocket;
   7 |
>  8 |     beforeAll((done) => {
     |     ^
   9 |         const httpServer = createServer();
  10 |         io = new Server(httpServer);
  11 |         httpServer.listen(() => {

  at src/lib/socket-validator/backend-socket-validator-2.spec.ts:8:5
  at Object.<anonymous> (src/lib/socket-validator/backend-socket-validator-2.spec.ts:5:1)
  at TestScheduler.scheduleTests (../../../node_modules/@jest/core/build/TestScheduler.js:333:13)
  at runJest (../../../node_modules/@jest/core/build/runJest.js:401:19)
  at _run10000 (../../../node_modules/@jest/core/build/cli/index.js:320:7)
  at runCLI (../../../node_modules/@jest/core/build/cli/index.js:173:3)

● Test suite failed to run

ReferenceError: setImmediate is not defined

  at XMLHttpRequest.dispatchEvent (../../../node_modules/xmlhttprequest-ssl/lib/XMLHttpRequest.js:628:9)
  at setState (../../../node_modules/xmlhttprequest-ssl/lib/XMLHttpRequest.js:654:12)
  at IncomingMessage.<anonymous> (../../../node_modules/xmlhttprequest-ssl/lib/XMLHttpRequest.js:479:13)

Expected behavior I’d like the tests to pass.

Platform:

  • OS: Windows 10

Additional context These are all the dependencies I have related to Jest: “@nrwl/jest”: “13.1.4”, “@types/jest”: “^27.0.3”, “eslint-plugin-jest”: “^24.4.2”, “jest”: “27.3.1”, “jest-extended”: “^0.11.5”, “jest-preset-angular”: “9.0.7”, “ts-jest”: “27.0.7”,

… and to Socket.IO(/sockets)

@nestjs/platform-socket.io”: “^8.2.3”, “@nestjs/websockets”: “^8.2.3”, “socket.io”: “^4.4.0”, “socket.io-client”: “^4.4.0”,

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
rujorgensencommented, Dec 1, 2021

So it seems something in your setup hints jest to use jsdom, but I wasn’t able to find what.

You’re right, I generated a clean configuration using the node environment to compare against, and I was indeed missing testEnvironment: 'node' in my jest.config.js. Thanks a lot for you help 👌. Closing.

0reactions
akaigyouhoucommented, Jul 27, 2022

I’m reading a book written by Lucas da Costa, “Testing JavaScript Applications”. In this book, there is a scenario, which is to build a socket server to test the frontend code. In that case, because dom manipulation is required in test file. So the environment cannot be switched to node from jsdom. The code is here: https://github.com/lucasfcosta/testing-javascript-applications/blob/master/chapter6/5_web_sockets_and_http_requests/2_web_sockets/socket.test.js

Read more comments on GitHub >

github_iconTop Results From Across the Web

any Jest test fails with "result.message is not a function" after ...
After upgrading Jest to v.27.2.5 from v.26 if a test fails it shows me: result.message is not a function.
Read more >
From v27 to v28 - Jest
Upgrading Jest from v27 to v28? This guide aims to help refactoring your configuration and tests.
Read more >
Sonar is showing 0% code coverage with QUnit/JavaScript but ...
I'm using SONARQUBE locally v. 8.8 and on our Jenkins Server v.7.9 I've created QUnit tests because we're using AEM (Adobe Experience ...
Read more >
Mockito (Mockito 4.10.0 API) - javadoc.io
In the lifecycle of a library, breaking changes are necessary to roll out a set ... You can continue to run the same...
Read more >
How To Upgrade From Selenium 3 To Selenium 4?
The SIDE Runner tool in Selenium 4 IDE lets you run Selenium tests parallel on local Selenium Grid and cloud-based Selenium Grid. The...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found