Skip to content

Commit

Permalink
test for destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
imcotton committed Jan 16, 2020
1 parent 5860933 commit 9858ede
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions test/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Readable, pipeline, PassThrough } from 'stream';

import { asyncReadable, AsyncReadable } from '../lib/async-readable';
import { reader } from '../lib/utils';





describe('reader', () => {

const source = Buffer.from([ 1, 2, 3, 4, 5 ]);

const readable = new Readable({

highWaterMark: 1,

read () {
this.push(source);
this.push(null);
},

});

class Oops extends Error {}

const gen = async function* ({ read }: AsyncReadable) {
yield* await Promise.all([ read(1), read(1), read(1) ]);
yield read(1);
throw new Oops();
yield read(1);
};


test('error w/ destroy', done => {

const fn = jest.fn();

const stream = new Readable({
objectMode: false,
read: reader(gen(asyncReadable(readable)), fn),
});

pipeline(stream, new PassThrough(), error => {

expect(fn).toHaveBeenCalled();
expect(error).toBeInstanceOf(Oops);

done();

});

});

});

0 comments on commit 9858ede

Please sign in to comment.