A small utility to capture console output in mocha tests
npm install mocha-streambufferjs
const StreamBuffer = require('mocha-streambuffer');
const assert = require('assert');var stdout;
describe('StreamBuffer', function()
{
before(function()
{
stdout = new StreamBuffer(process.stdout, true).hook();
});
beforeEach(function()
{
stdout.clean();
});
after(function()
{
stdout.unhook();
});
describe('Catch', function()
{
it('should catch stdout', function()
{
console.log('ping');
assert.equal(stdout.buffer(), 'ping\n');
});
});
describe('Includes', function()
{
it('should include ping', function()
{
console.log('ping');
assert.ok(stdout.buffer().includes('ping'));
});
});
});
``StreamBuffer.hook() => this
Starts buffering
StreamBuffer.unhook() => this
Stops buffering
StreamBuffer.buffer() => String
Returns the buffer
StreamBuffer.clean() => String
Resets the buffer and returns it