Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/doowb/capture-stream
Capture stream output.
https://github.com/doowb/capture-stream
node-streams nodejs stderr stdio stdio-stream stdout streams test testing tests unit-testing unittest
Last synced: 13 days ago
JSON representation
Capture stream output.
- Host: GitHub
- URL: https://github.com/doowb/capture-stream
- Owner: doowb
- License: mit
- Created: 2015-09-23T12:59:05.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2020-12-21T09:24:52.000Z (almost 4 years ago)
- Last Synced: 2024-10-04T16:42:29.597Z (about 1 month ago)
- Topics: node-streams, nodejs, stderr, stdio, stdio-stream, stdout, streams, test, testing, tests, unit-testing, unittest
- Language: JavaScript
- Size: 11.7 KB
- Stars: 11
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# capture-stream [![NPM version](https://img.shields.io/npm/v/capture-stream.svg)](https://www.npmjs.com/package/capture-stream) [![Build Status](https://img.shields.io/travis/doowb/capture-stream.svg)](https://travis-ci.org/doowb/capture-stream)
> Capture stream output.
## Install
Install with [npm](https://www.npmjs.com/):
```sh
$ npm install capture-stream --save
```## Usage
```js
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.
```js
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.
```js
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](index.js#L27)
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**
```js
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' ] ]
```## Related projects
* [composer-errors](https://www.npmjs.com/package/composer-errors): Listen for and output Composer errors. | [homepage](https://github.com/doowb/composer-errors)
* [composer-runtimes](https://www.npmjs.com/package/composer-runtimes): Write composer task start and end times to a stream. | [homepage](https://github.com/doowb/composer-runtimes)## Contributing
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/doowb/capture-stream/issues/new).
## Building docs
Generate readme and API documentation with [verb][]:
```sh
$ npm install verb && npm run docs
```Or, if [verb][] is installed globally:
```sh
$ verb
```## Running tests
Install dev dependencies:
```sh
$ npm install -d && npm test
```## Author
**Brian Woodward**
* [github/doowb](https://github.com/doowb)
* [twitter/doowb](http://twitter.com/doowb)## License
Copyright © 2016 [Brian Woodward](https://github.com/doowb)
Released under the [MIT license](https://github.com/doowb/capture-stream/blob/master/LICENSE).***
_This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on March 19, 2016._