0

I am trying to make simple chat app using socket.io. Manually it's working fine (via browser localhost:3000/) but I want to write unit tests to verify logic of the server. I don't know which tools/libraries to use for testing.

Here is my node server code:

index.js

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

var socketCount = 0;

http.listen(3000, function(){
  console.log('listening on *:3000');
});

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket) {

  socketCount++;

  console.log('new user connected');

  // Let all sockets know how many are connected
  io.emit('users connected', socketCount);

    socket.on('disconnect', function(){
    // Decrease the socket count on a disconnect, emit
    socketCount--;
    io.emit('users connected', socketCount);
    });

  socket.on('chat message', function(msg){
    // New message added, broadcast it to all sockets
    io.emit('chat message', msg);
  });

});

And here is my html page:

file: index.html

<html>
...
<body>
    <ul id="messages"></ul>
    <div class='footer'>
      <div id="usersConnected"></div>
      <form action="">
        <input id="m" autocomplete="off" /><button>Send</button>
      </form>
    </div>
  </body>
  ...
  <script>
    $(document).ready(function(){
        $('#messages').val('');
        var socket = io();

        $('form').submit(function(){
          socket.emit('chat message', $('#m').val());
          $('#m').val('');
          return false;
        });

        // New message emitted, add it to our list of current messages
        socket.on('chat message', function(msg){
          $('#messages').append($('<li>').text(msg));
        });

        // New socket connected, display new count on page
        socket.on('users connected', function(count){
          $('#usersConnected').html('Users connected: ' + count);
        });
    });
  </script>
</html>

Thanks.

1 Answer 1

1

I had this problem: How to do unit test with a "socket.io-client" if you don't know how long the server take to respond?.

I've solved so using mocha and chai:

var os = require('os');
var should = require("chai").should();
var socketio_client = require('socket.io-client');

var end_point = 'http://' + os.hostname() + ':8081';
var opts = {forceNew: true};

describe("async test with socket.io", function () {
this.timeout(10000);

it('Response should be an object', function (done) {
    setTimeout(function () {
        var socket_client = socketio_client(end_point, opts);  

        socket_client.emit('event', 'ABCDEF');

        socket_client.on('event response', function (data) {
            data.should.be.an('object');
            socket_client.disconnect();
            done();
        });

        socket_client.on('event response error', function (data) {
            console.error(data);
            socket_client.disconnect();
            done();
            });
        }, 4000);
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Finally found a unit-testing example for socketIO that actually works.

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.