Answer the question
In order to leave comments, you need to log in
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();
});
...
})
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)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question