Mock nodejs streams.
- Create a readable stream from any iterable.
- Create a writable stream that puts its data at your disposal.
- Create a duplex stream that combines a readable and writable stream together.
- Can operate both in object and normal ( Buffer ) mode.
yarn add stream-mock
Or, if you are more a npm
person
npm i stream-mock
You are building an awesome brand new Transform stream that rounds all your values.
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.
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
MIT