Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocha Monitor Application Output

I'm building a logging module for my web app in nodejs. I'd like to be able to test using mocha that my module outputs the correct messages to the terminal. I have been looking around but haven't found any obvious solutions to check this. I have found

process.stdout.on('data', function (){})

but haven't been able to get this to work. does anybody have any advice?

like image 449
joshua-anderson Avatar asked Aug 30 '13 23:08

joshua-anderson


1 Answers

Two other libraries that help with this are test-console and intercept-stdout I haven't used intercept-stdout, but here's how you can do it with test-console.

var myAsync = require('my-async');
var stdout = require('test-console').stdout;

describe('myAsync', function() {
  it('outputs something', function(done) {
    var inspect = stdout.inspect();

    myAsync().then(function() {
      inspect.restore();
      assert.ok(inspect.output.length > 0);
      done();
    });
  });
});

Note: You must use Mocha's async api. No calling done() will swallow mocha's test messaging.

like image 58
ianstarz Avatar answered Oct 13 '22 21:10

ianstarz