Upgrading from 4.1.3 to 4.4.0 breaks my Jest unit tests
See original GitHub issueDescribe 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:
- Created 2 years ago
- Comments:7 (3 by maintainers)
Top GitHub Comments
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.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