I'm building a little command line tool and I hit a problem with testing.
How can test that the current process wrote "omg" in stderr?
process.stderr.write("omg")
Install Mocha if you haven't:
npm install -g mocha
shouldpass.js:
process.stderr.write('omg')
shouldfail.js:
process.stdout.write('not omg on stderr')
test.js:
var exec = require('child_process').exec
, assert = require('assert')
describe('run tests', function(){
it('should pass', function(done) {
exec('node ./shouldpass.js', function(err, stdout, stderr) {
assert.equal(stderr, 'omg')
done()
})
})
it('should fail', function(done) {
exec('node ./shouldfail.js', function(err, stdout, stderr) {
assert.equal(stderr, 'omg')
done()
})
})
})
To run:
mocha test.js