A
A
Aliansys2014-03-17 17:55:44
Node.js
Aliansys, 2014-03-17 17:55:44

Is it possible to test Node.js + Socket.io server using Mocha?

There is a Node.js server. Communication between the client and the server takes place using socket.io.
Mocha has not been used before. You need to write tests for "request-response". That is, I want to check a specific response for a specific request.
The only thing that was found in Google:
liamkaufman.com/blog/2012/01/28/testing-socketio-w...
But here the case is described when the server and tests are described inside the test directory and run as part of Mocha's execution.
I already have a server. I want to write tests for a ready server just to make it easier to add new functionality in the future by automatically testing all the functionality at once. That is, I will have a test directory on the server, in which there will be all tests for the same server.
Based on what I could google, I wrote the following test code:

var assert = require("assert"),
  should = require('should'),
  io = require('socket.io-client'),
  socketURL = 'http://localhost:3102',
  options ={
  transports: ['websocket'],
  'force new connection': true,
  'reconnection delay' : 0,
  'reopen delay' : 0
  };
describe('Server', function(){
  var socket;
  beforeEach(function(done) {
    // Setup
    console.log('Establishing connection');
    socket = io.connect(socketURL, options);

    socket.on('connect', function() {
      console.log('worked...');
      done();
    });
});
...
  it('should receive messages', function(done){
    socket.on('message', function(data){
      console.log('received');
      done();
    });
...
})

Test result:
Server
Establishing connection
    1) "before each" hook

  0 passing (2s)
  1 failing

  1) Server "before each" hook:
     Error: timeout of 2000ms exceeded
      at null.<anonymous> (\npm\node_modules\mocha\lib\runnable.js:139:19)
      at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)

Nothing in the server log. Nothing tried to connect to it.
If you yourself go through the web interface, then the sockets normally connect to the server. That is, the server itself is functioning normally.
Is such testing possible with Mocha, in principle? Or am I doing/understanding something wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aliansys, 2014-03-18
@Aliansys

Answer found. The versions of the socket.io and socket.io-client libraries must be the same (well, or something is wrong in the new version of socket.io-client).
Now I have socket.io v0.9.16 and socket.io-client of the same version and everything works fine.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question