-
-
Notifications
You must be signed in to change notification settings - Fork 3k
HOW TO: Correctly stub stdout
Craig Taub edited this page Jun 17, 2019
·
2 revisions
If you want to stub stdout inside your own code (via process.stdout.write
or console.log
) the problem is it will effect Mochas reporters, which rely on the same mechanisms to print results of the tests.
i.e.
it('should do, but it do not', function() {
// user wants to hide output generated by console.log calls in fn 'foo'
console.log = function() {};
foo();
expect(...)
});
This will result in a faulty output.
The correct way to handle this is to stub before and restore after the function call.
it('will do', function() {
var consoleLogStub = sinon.stub(console, 'log');
foo();
consoleLogStub.restore();
expect(...)
});
Now the reporters can run and print the assertion without any interference, while stubbing stdout inside your function.