Skip to content

Commit

Permalink
test(stream-transform-from): add tests that inherit from the `Transfo…
Browse files Browse the repository at this point in the history
…rmFromAsyncIterable` class
  • Loading branch information
sounisi5011 committed May 21, 2021
1 parent 38d4cfe commit 28e9901
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 29 deletions.
1 change: 1 addition & 0 deletions packages/stream-transform-from/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ module.exports = {
},
testEnvironment: 'node',
testMatch: ['<rootDir>/tests/**/*.ts'],
testPathIgnorePatterns: ['<rootDir>/tests/helpers/'],
};
6 changes: 3 additions & 3 deletions packages/stream-transform-from/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type GetPropValue<T, K extends PropertyKey> = K extends (keyof T) ? T[K] : undef
* If the `objectMode` and `writableObjectMode` options is not `true`,
* the chunk value is always an instance of Buffer.
*/
type InputChunkType<T extends stream.TransformOptions> = (
export type InputChunkType<T extends stream.TransformOptions> = (
true extends (GetPropValue<T, 'objectMode'> | GetPropValue<T, 'writableObjectMode'>) ? unknown : Buffer
);

Expand All @@ -19,12 +19,12 @@ type IfNeverThenUnknown<T> = [T] extends [never] ? unknown : T;
* the chunk value must be of type string or an instance of Buffer or Uint8Array.
* @see https://github.com/nodejs/node/blob/v12.17.0/lib/_stream_readable.js#L226-L244
*/
type OutputChunkType<T extends stream.TransformOptions> = IfNeverThenUnknown<
export type OutputChunkType<T extends stream.TransformOptions> = IfNeverThenUnknown<
T extends ({ objectMode: true } | { readableObjectMode: true }) ? never
: string | Buffer | Uint8Array
>;

type TransformFunction<TOpts extends stream.TransformOptions> = (
export type TransformFunction<TOpts extends stream.TransformOptions> = (
source: AsyncIterableIterator<InputChunkType<TOpts>>,
) => Iterable<OutputChunkType<TOpts>> | AsyncIterable<OutputChunkType<TOpts>>;

Expand Down
36 changes: 36 additions & 0 deletions packages/stream-transform-from/tests/extend-class.ts
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);
});
});
27 changes: 27 additions & 0 deletions packages/stream-transform-from/tests/helpers/index.ts
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();
},
});
}
27 changes: 1 addition & 26 deletions packages/stream-transform-from/tests/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,7 @@ import * as stream from 'stream';
import { promisify } from 'util';

import { transformFrom } from '../src';

function assertType<T>(_: T): void {
//
}

function createNoopWritable(opts?: Omit<stream.WritableOptions, 'write'>): stream.Writable {
return new stream.Writable({
...opts,
write(_chunk, _, done) {
done();
},
});
}

function createOutputWritable(
outputChunkList: unknown[],
opts?: Omit<stream.WritableOptions, 'write'>,
): stream.Writable {
return new stream.Writable({
...opts,
write(chunk, _, done) {
outputChunkList.push(chunk);
done();
},
});
}
import { assertType, createNoopWritable, createOutputWritable } from './helpers';

describe('passes though chunks', () => {
const data = ['first', 'second', 'third'];
Expand Down

0 comments on commit 28e9901

Please sign in to comment.