简体   繁体   中英

Testing with Mocha in Node.js

I'm getting really inconsistent behavior in my terminal when console.logging inside of a test I wrote using mocha. We are running a node.js server and running socket.io. Does the console.log not go to the terminal only some of the time for some reason? I'm really confused about this behavior.

➜  tests git:(master) ✗ mocha test-chat-server.js
hello world


echo
✓ echos message (68ms)

On Connect Things Should Happen
✓ initial connection events
  disconnected
  i'm here


    2 passing (93ms)

➜  tests git:(master) ✗ mocha test-chat-server.js
  hello world


    echo
      ✓ echos message (61ms)

    On Connect Things Should Happen
      ✓ initial connection events


    2 passing (77ms)

The difference between these two times I ran the mocha test are the console.log statements that appears in the first test run (disconnected, i'm here). They do not appear in the second test that I ran.

Edit: posting my test code in response to the comment (thank you!)

var should = require('should');
var socket = require('socket.io-client')('http://localhost:3000');


describe("echo", function () {
    var server,
        options ={
            transports: ['websocket'],
            'force new connection': true
        };

    it("echos message", function (done) {
        var client = socket.connect("http://localhost:3000", options);

        client.once("connect", function () {
            client.once("echo", function (message) {
                message.should.equal("Hello World");

                client.disconnect();
                done();
            });

            client.emit("echo", "Hello World");
        });
        done();
    });
});

describe("On Connect Things Should Happen", function() {

    it('initial connection events', function() {

        should.exist(socket); 
        socket.open();      
        socket.compress(false).emit('an event', { some: 'data' });

        socket.on('error', function() {
            console.log('error'); 
        }); 

        socket.connect(); 
        socket.on('disconnect', function(connection) {
            console.log('disconnected'); 
            console.log("i'm here");
        });



        socket.on('connect', function(connection) {
            console.log('connected'); 
        });
    });
});

You are falling into the classic node async trap. Your "things should happen" test sometimes returns before the disconnect event happens and sometimes not.

You need to handle the done function the same way you do in the "echoes message" test. Punctually, it should like this:

socket.on('disconnect', function(connection) { 
console.log('disconnected'); 
console.log("i'm here"); 
done()});

In general, I'm not sure how much that test makes handling all those different callbacks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM