3

I need to write unit test using sinon to web socket client. the code is as following:

Socket = {
    connect: function ()
    {
        socket = new WebSocket('ws://localhost:12345');
        socket.onopen = function()
        {

            console.log('connected to the server');
        };

        socket.onmessage = function(message)
        {
            console.log('Received:', message.data);
        };

    }
};
3
  • What's your question? Commented Jul 23, 2013 at 9:35
  • Provide some more clarity and add the sinon tag Commented Jul 23, 2013 at 9:53
  • possible duplicate of how should I test client/server socket.io in mocha? Commented Oct 11, 2013 at 2:00

1 Answer 1

1

We need return the socket instance at last in the connect method. Because you assigned two new functions to onopen and onmessage events. It will override the spy or stub methods on the socket object.

Test environment: Node

Here is the unit test solution:

index.js:

const Socket = {
  connect: function() {
    socket = new WebSocket("ws://localhost:12345");
    socket.onopen = function() {
      console.log("connected to the server");
    };

    socket.onmessage = function(message) {
      console.log("Received:", message.data);
    };
    return socket;
  }
};

module.exports = Socket;

index.spec.js:

const sinon = require("sinon");
const { expect } = require("chai");
const Socket = require("./index");

class WebSocket {
  constructor(uri) {}
  onopen() {}
  onmessage() {}
}
global.WebSocket = WebSocket;

describe("17806481", () => {
  it("should test connect correctly", () => {
    const logSpy = sinon.spy(console, "log");
    const socket = Socket.connect();
    const onopenSpy = sinon.spy(socket, "onopen");
    const onmessageSpy = sinon.spy(socket, "onmessage");
    onopenSpy();
    expect(logSpy.firstCall.calledWith("connected to the server")).to.be.true;
    const mMessage = { data: "fake data" };
    onmessageSpy(mMessage);
    expect(logSpy.secondCall.calledWith("Received:", mMessage.data)).to.be.true;
  });
});

Unit test result with 100% coverage for Socket module:

 17806481
connected to the server
Received: fake data
    ✓ should test connect correctly


  1 passing (10ms)

---------------|----------|----------|----------|----------|-------------------|
File           |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
---------------|----------|----------|----------|----------|-------------------|
All files      |      100 |      100 |       75 |      100 |                   |
 index.js      |      100 |      100 |      100 |      100 |                   |
 index.spec.js |      100 |      100 |       60 |      100 |                   |
---------------|----------|----------|----------|----------|-------------------|

Source code: https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/17806481

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.