title: How to test a basic socket.io Node.js app using Jest library?
body: <p>I am using Jest testing library for my react.js project. I want to use it to test my Node.js application as well, I have tried many examples out there but still all of jest examples do not work.</p>
  
  <p>One example of testing socket.io connection with Mocha &amp; Chai worked, but when I use Jest, it is connected successfully to the socket.io server, and the events are emitted, but could not be listened to.
  I have also updated socket.io package in both cases.</p>
  
  <p>here's the example using mocha &amp; chai <a href="https://github.com/agconti/socket.io.tests" rel="noreferrer">https://github.com/agconti/socket.io.tests</a></p>
  
  <p>and here's my code: </p>
  
  <p>index.js</p>
  
  <pre><code>var express = require("express"),
    app = express(),
    port = process.env.PORT || 9000,
    http = require("http").Server(app),
    io = require("socket.io")(http);
  
  io.on("connection", function(socket) {
    console.log("connected");
  
    socket.on("message", function(msg) {
      console.log("emitted");
  
      io.sockets.emit("message", msg);
    });
  });
  
  // export the server so it can be easily called for testing
  exports.server = http.listen(port);
  </code></pre>
  
  <p>index.test.js</p>
  
  <pre><code>jest.setTimeout(50000);
  var server = require("../index"),
    io = require("socket.io-client"),
    ioOptions = {
      transports: ["websocket"],
      forceNew: true,
      reconnection: false
    },
    testMsg = "HelloWorld",
    sender,
    receiver;
  
  describe("Chat Events", function() {
    beforeEach(function(done) {
      // start the io server
      // server.start();
      // connect two io clients
      sender = io("http://localhost:9000/", ioOptions);
      receiver = io("http://localhost:9000/", ioOptions);
  
      // finish beforeEach setup
      done();
    });
    afterEach(function(done) {
      // disconnect io clients after each test
      sender.disconnect();
      receiver.disconnect();
      done();
    });
  
    describe("Message Events", function() {
      it("Clients should receive a message when the `message` event is emited.", function(done) {
        sender.emit("message", testMsg);
        receiver.on("message", function(msg) {
          console.log("subscribed");
          expect(msg).toEqual(testMsg);
          done();
        });
      });
    });
  });
  </code></pre>
  
  <p>here's the test result:</p>
  
  <p>JEST</p>
  
  <pre><code> console.log server/index.js:37
      connectedddd
  
    console.log server/index.js:40
      emitted
  
    console.log server/index.js:37
      connectedddd
  
    console.log server/index.js:37
      connectedddd
  
   FAIL  server/__tests__/index.test.js (58.299s)
    Chat Events
      Message Events
        ✕ Clients should receive a message when the `message` event is emited. (50105ms)
  
    ● Chat Events › Message Events › Clients should receive a message when the `message` event is emited.
  
      Timeout - Async callback was not invoked within the 50000ms timeout specified by jest.setTimeout.Error: 
  
        130 | 
        131 |   describe("Message Events", function() {
      &gt; 132 |     it("Clients should receive a message when the `message` event is emited.", function(done) {
            |     ^
        133 |       sender.emit("message", testMsg);
        134 |       receiver.on("message", function(msg) {
        135 |         console.log("subscribed");
  
        at new Spec (node_modules/jest-jasmine2/build/jasmine/Spec.js:116:22)
        at Suite.it (server/__tests__/index.test.js:132:5)
        at Suite.describe (server/__tests__/index.test.js:131:3)
        at Object.describe (server/__tests__/index.test.js:113:1)
  
  
  </code></pre>
  
  <p>MOCHA &amp; CHAI</p>
  
  <pre><code>
  &gt; node_socket_test@0.0.0 test /home/hassan/socket.io.tests
  &gt; mocha test
  
  
  
    Chat Events
      Message Events
  connectedddd
  connectedddd
  subscribed
        ✓ Clients should receive a message when the `message` event is emited.
  
  
    1 passing (53ms)
  </code></pre>