Search code examples
node.jstddassertmocha.js

Assertion in event brake down Mocha when run programmatically


I have problem with Mocha. If i run this programmaticaly from Jake Mocha brakes down and don't show nothing more than some errors stuff like:

AssertionError: There is a code 200 in response
    at Socket.<anonymous> (/home/X/Y/Z/test/test_Server.js:70:4)
    at Socket.EventEmitter.emit (events.js:93:17)
    at TCP.onread (net.js:418:51)

Runned from command line gives more expected results. That is:

19 passing (30ms)
  7 failing

  1) RTDB accepts connection with package and response with code 200 if correct package was send:
     Uncaught AssertionError: There is a code 200 in response
      at Socket.<anonymous> (/X/Y/Z/test/test_Server.js:70:4)
      at Socket.EventEmitter.emit (events.js:93:17)
      at TCP.onread (net.js:418:51)

  2) XYZ should be able to store GHJ for IJS:
     Error: expected f...
...

The problem is following code:

test('accepts connection with package and response with code 400 ' +
        'if wrong package was send', function (done) {
    console.log('client connecting to server');
    var message = '';
    var client = net.connect(8122, 'localhost', function () {
        client.write('Hello');
        client.end();
    } );
    client.setEncoding('utf8');

    client.on('data', function (data) {
        message += data;
    } );

    client.on('end', function (data) {
        assert(message.indexOf('400') !== -1, 'There is a code 400 in response');
        done();
    });

    client.on('error', function(e) {
        throw new Error('Client error: ' + e);
    });
});

If I do

assert(message.indexOf('400') !== -1, 'There is a code 400 in response');

just after

var message = '';

Mocha fails correctly (I mean displaying errors etc.), So this is fault of asynch assertion done on event. How can I correct that? Thats real problem Because this test is first, and I get no clue where to look for source of problem (If there is any). Should I somehow catch this assertion error and pass it to Mocha?

EDIT: Answer to comment how is Jake running Mocha - just like that:

var Mocha = require('mocha');
...
task("test", [], function() {
    // First, you need to instantiate a Mocha instance.
    var mocha = new Mocha({
        ui: 'tdd',
        reporter: 'dot'
    });

    // Then, you need to use the method "addFile" on the mocha
    // object for each file.
    var dir = 'test';

    fs.readdirSync(dir).filter(function(file){
        // Only keep the .js files
        return file.substr(-3) === '.js';

    }).forEach(function(file){
        // Use the method "addFile" to add the file to mocha
        mocha.addFile(
            path.join(dir, file)
        );
    });

    // Now, you can run the tests.
    mocha.run(function(failures){
        if(failures){
            fail("Mocha test failed");
        } else {
            complete();
        }
    });
}, {async: true});

Solution

  • Based on this answer: https://stackoverflow.com/a/9132271/2024650

    In few words: I remove listener on uncaughtException in Jake. This allow Mocha to handle this uncaughtExceptions. At the end I add back this listener.

    This solves my answer for now:

    task("test", [], function() {
    
        var originalExeption = process.listeners('uncaughtException').pop();
        //!!!in node 0.10.X you should also check if process.removeListener isn't necessary!!!
    
        console.log(originalExeption);
    
        // First, you need to instantiate a Mocha instance.
        var mocha = new Mocha({
            ui: 'tdd',
            reporter: 'dot'
        });
    
        // Then, you need to use the method "addFile" on the mocha
        // object for each file.
        var dir = 'test';
    
        fs.readdirSync(dir).filter(function(file){
            // Only keep the .js files
            return file.substr(-3) === '.js';
    
        }).forEach(function(file){
            // Use the method "addFile" to add the file to mocha
            mocha.addFile(
                path.join(dir, file)
            );
        });
    
        // Now, you can run the tests.
        mocha.run(function(failures){
            if(failures){
                fail("Mocha test failed");
            } else {
                complete();
            }
            process.listeners('uncaughtException').push(originalExeption);
    
    
        });
    }, {async: true});