How to write Jest unit testing for Socket in node js

Viewed 28

I am trying to write the unit testing in JEST for the socket, test cases are passed but the code is not covered on codeCoverageReport I am not sure where I have gone wrong and jest is completely new as I am in a learning stage please help me.

This is node js simple code of socket

const express = require("express");
const app = express();
items = [];

const server = app.listen(3000, () => {
  console.log("This is listening to the port");
});

//Web Sockets
const io = require("socket.io")(server, {
  cors: {
    origin: "http://localhost:4200",
    methods: ["GET", "POST"],
  },
});
io.on("connection", (socket) => {
  console.log("a user connected");

  socket.on("message", (message) => {
    console.log(message);
    io.emit("message", `${message}`);
  });

  socket.on("disconnect", () => {
    console.log("a user disconnected!");
  });
});
module.exports = io;

This is the test file for the Socket

const io = require('socket.io-client')
const io_server = require('../src/socket/index');

describe('basic socket.io example', () => {

  let socket;

  beforeEach(() => {
    // Setup
    socket = io.connect('http://localhost:3000');

    socket.on('connect', () => {
      console.log('connected')
    });

    socket.on('disconnect', () => {
      console.log('disconnected...');
    });
  });

  afterEach((done) => {
    // Cleanup
    if (socket.connected) {
      socket.disconnect();
    }
    socket.close();
    done();
  });

  it('should communicate', () => {
    // once connected, emit Hello World

    // console.log(socket)
    socket.emit('echo', 'Hello World');

    socket.once('echo', (message) => {
      // Check that the message matches
      expect(message).to.equal('Hello World');
    });

    socket.on('connection', (socket) => {
      expect(socket).to.not.be.null;
    });
  });

});

Below is the screenshot of the code coverage

enter image description here

When I run the test case it is showing it has passed

enter image description here

1 Answers

See the official testing recipes

Solution:

server.js:

const express = require('express');
const app = express();

const server = app.listen(3000, () => {
  console.log('This is listening to the port');
});

const io = require('socket.io')(server);
io.on('connection', (socket) => {
  console.log('a user connected');

  socket.on('message', (message) => {
    console.log(message);
    io.emit('message', `${message}`);
  });

  socket.on('disconnect', () => {
    console.log('a user disconnected!');
  });
});
module.exports = io;

server.test.js:

const ioc = require('socket.io-client');
const assert = require('assert');

describe('73795747', () => {
  let io, clientSocket;
  before((done) => {
    io = require('./server');
    clientSocket = ioc('http://localhost:3000');
    clientSocket.on('connect', done);
  });
  after(() => {
    io.close();
    clientSocket.close();
  });

  it('should connect socket server and emit message', (done) => {
    clientSocket.on('message', (data) => {
      assert.strictEqual(data, 'Hello World!');
      done()
    });
    clientSocket.emit('message', 'Hello World!');
  });
});

Test result:

  73795747
This is listening to the port
a user connected
Hello World!
    ✓ should connect socket server and emit message
a user disconnected!


  1 passing (287ms)

-----------|---------|----------|---------|---------|-------------------
File       | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
-----------|---------|----------|---------|---------|-------------------
All files  |     100 |      100 |     100 |     100 |                   
 server.js |     100 |      100 |     100 |     100 |                   
-----------|---------|----------|---------|---------|-------------------

package versions:

"socket.io": "^2.3.0",
"socket.io-client": "^2.5.0",
"express": "^4.17.1",
Related