Wednesday, 7 June 2017

NodeJS Mocha Chai Unit Testing Socket Emit After Chai Request To Node Event

When unit testing a node event I am unable to confirm the success of the route event by checking the socket emit. Below is my sample code and I think it is self-explanatory.

Issue facing is: This piece of code is working, and which is the proof of the success of the event being called.

 `server.on('gotData',function(){
     console.log('event emitted');
     assert(true);
     done();
   });`

Node Code:

router.post('/send', jsonParser, function (req, res) {
      //some logic here
      io.to(socketId).emit('gotData', data);
});

Client side

var socket = io.connect(serverUrl);
socket.on('gotData', function(data){
      console.log('Data got in client side');                         
});

Test Suite:

it('should be sent event to client-side when valid object has provided', function (done) {
   chai.request(server)
                .post('/send')
                .set({'Content-Type':'application/json'})
                .send(data);

   var socket;
   socket = io.connect(server);
   server.on('gotData',function(){
     console.log('event emitted');
     assert(true);
     done();
   });
});



via Sujathan R

No comments:

Post a Comment