JSPM

capture-stream

0.1.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 108
  • Score
    100M100P100Q81339F
  • License MIT

Capture stream output.

Package Exports

  • capture-stream

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (capture-stream) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

capture-stream NPM version Build Status

Capture stream output.

Install with npm

$ npm i capture-stream --save

Usage

var capture = require('capture-stream');
var restore = capture(process.stdout);

console.log('Hello, world!!!');
console.log('foo', 'bar');

var output = restore();
console.log(output);
//=> [ [ 'Hello, world!!!\n' ], [ 'foo bar\n' ] ]

Pass true to restore to return a string instead of an array of output.

var capture = require('capture-stream');
var restore = capture(process.stdout);

console.log('Hello, world!!!');
console.log('foo', 'bar');

var output = restore(true);
console.log(output);
//=> Hello, world!!!
//=> foo bar
//=>

This module has been built to be used in unit tests to easily capture output from process.stdout and process.stderr and test the results.

describe('awesome module', function () {
  function log () {
    console.log.apply(console, arguments);
  }

  it('should write "Hello, world!!!" to stdout', function () {
    var restore = capture(process.stdout);
    log('Hello, world!!!');
    var output = restore();
    assert.equal(output.length, 1);
    assert(output[0][0].indexOf('Hello, world!!!') === 0);
  });
});

API

captureStream

Capture the output from a stream and store later.

Params

  • stream {Stream}: A stream to capture output from (e.g. process.stdout, process.stderr)
  • returns {Function} restore: function that restores normal output and returns an array of output.

Example

var restore = capture(process.stdout);
console.log('Hello, world!!!');
console.log('foo', 'bar');

var output = restore();
console.log(output);
//=> [ [ 'Hello, world!!!\n' ], [ 'foo bar\n' ] ]

Running tests

Install dev dependencies:

$ npm i -d && npm test

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Author

Brian Woodward

License

Copyright © 2015 Brian Woodward Released under the MIT license.


This file was generated by verb-cli on September 28, 2015.