stream-mock
Version:
Node stream mock module
97 lines (73 loc) • 3.15 kB
Markdown
[](https://travis-ci.org/b4nst/stream-mock)
[](https://www.npmjs.com/package/stream-mock)
[](https://github.com/b4nst/stream-mock/network/alerts)
[](https://codeclimate.com/github/b4nst/stream-mock)
[](https://codeclimate.com/github/b4nst/stream-mock)
[](https://greenkeeper.io/)



Mock nodejs streams.
- Create a
[](https://nodejs.org/api/stream.html#stream_readable_streams)
from any iterable.
- Create a
[](https://nodejs.org/api/stream.html#stream_writable_streams)
that puts its data at your disposal.
- Create a
[](https://nodejs.org/api/stream.html#stream_duplex_and_transform_streams)
that combines a readable and writable stream together.
- Can operate both in
[](https://nodejs.org/api/stream.html#stream_object_mode) and normal
( [Buffer](https://nodejs.org/api/buffer.html#buffer_buf_length) ) mode.
```shell
yarn add stream-mock
```
Or, if you are more a `npm` person
```shell
npm i stream-mock
```
You are building an awesome brand new
[](https://nodejs.org/api/stream.html#stream_duplex_and_transform_streams)
that rounds all your values.
```javascript
import { Transform } from 'stream';
export default class Rounder extends Transform {
_transform(chunk, encoding, callback) {
this.push(Math.round(chunk));
callback();
}
}
```
Now you need / want to test it.
```javascript
import { ObjectReadableMock, ObjectWritableMock } from 'stream-mock';
import chai from 'chai';
import Rounder from 'the/seven/bloody/hells';
chai.should();
describe('Test me if you can', (done) => {
it('Round me like one of your french girls', {
// Given
const input = [1.2, 2.6, 3.7];
const transform = new Rounder({objectMode: true});
const reader = new ObjectReadableMock(input);
const writer = new ObjectWritableMock();
// When
reader.pipe(transform).pipe(writer);
// Then
writer.on('finish', ()=>{
writer.data.should.deep.equal(input.map(Math.round));
})
});
});
```

Full API doc is hosted [here](https://bastienar.github.io/stream-mock/)
----------------
MIT