mocha-streambuffer

1.0.24 • Public • Published

mocha-streambuffer

A small utility to assert console output in mocha tests

Usage

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(stream, [verbose=true])

stream: The stream to buffer
verbose: Whether to still write to the stream

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

Credits

jjm for this excellent post on stackoverflow.

Package Sidebar

Install

npm i mocha-streambuffer

Weekly Downloads

3

Version

1.0.24

License

MIT

Unpacked Size

3.11 kB

Total Files

3

Last publish

Collaborators

  • stz