-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(stream-transform-from): add tests that inherit from the `Transfo…
…rmFromAsyncIterable` class
- Loading branch information
1 parent
38d4cfe
commit 28e9901
Showing
5 changed files
with
68 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import * as stream from 'stream'; | ||
import { promisify } from 'util'; | ||
|
||
import { TransformFromAsyncIterable, TransformFunction } from '../src'; | ||
import { createOutputWritable } from './helpers'; | ||
|
||
describe('inherited classes', () => { | ||
it('overwrite incoming chunks', async () => { | ||
const data = [0, 1, 2, 3]; | ||
const outputData = [1, 2, 3, 4]; | ||
|
||
class Increment extends TransformFromAsyncIterable<{ objectMode: true }> { | ||
constructor(f: TransformFunction<{ objectMode: true }>) { | ||
super(f, { objectMode: true }); | ||
} | ||
|
||
_transform(chunk: unknown, encoding: BufferEncoding, callback: stream.TransformCallback): void { | ||
super._transform( | ||
typeof chunk === 'number' ? chunk + 1 : chunk, | ||
encoding, | ||
callback, | ||
); | ||
} | ||
} | ||
|
||
const outputChunkList: unknown[] = []; | ||
await promisify(stream.pipeline)( | ||
stream.Readable.from(data), | ||
new Increment(async function*(source) { | ||
yield* source; | ||
}), | ||
createOutputWritable(outputChunkList), | ||
); | ||
expect(outputChunkList).toStrictEqual(outputData); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import * as stream from 'stream'; | ||
|
||
export function assertType<T>(_: T): void { | ||
// | ||
} | ||
|
||
export function createNoopWritable(opts?: Omit<stream.WritableOptions, 'write'>): stream.Writable { | ||
return new stream.Writable({ | ||
...opts, | ||
write(_chunk, _, done) { | ||
done(); | ||
}, | ||
}); | ||
} | ||
|
||
export function createOutputWritable( | ||
outputChunkList: unknown[], | ||
opts?: Omit<stream.WritableOptions, 'write'>, | ||
): stream.Writable { | ||
return new stream.Writable({ | ||
...opts, | ||
write(chunk, _, done) { | ||
outputChunkList.push(chunk); | ||
done(); | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters