Skip to content

Commit

Permalink
test(stream-transform-from): fix the bug that no error occurs after g…
Browse files Browse the repository at this point in the history
…etting all the chunks

This bug has been fixed in Node.js v15.

+ https://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V15.md#15.0.0
+ nodejs/node#34314

However, we have found a workaround that works for anything less than Node.js v15, so we will attempt to fix this bug in this package.
  • Loading branch information
sounisi5011 committed May 22, 2021
1 parent e56a0fa commit 40406fc
Showing 1 changed file with 44 additions and 34 deletions.
78 changes: 44 additions & 34 deletions packages/stream-transform-from/tests/public-api.ts
Expand Up @@ -578,47 +578,53 @@ describe('throw error from Transform', () => {
});

describe('when flush', () => {
const table: Array<[string, () => stream.Transform, boolean]> = [
const table: Array<[string, { createTransform: () => stream.Transform; hasBug: boolean }]> = [
[
'builtin Transform',
() =>
new stream.Transform({
transform(_chunk, _encoding, done) {
done();
},
flush(done) {
done(new Error('baz'));
},
}),
false,
{
createTransform: () =>
new stream.Transform({
transform(_chunk, _encoding, done) {
done();
},
flush(done) {
done(new Error('baz'));
},
}),
hasBug: false,
},
],
[
'builtin Transform (async done)',
() =>
new stream.Transform({
transform(_chunk, _encoding, done) {
done();
},
flush(done) {
setImmediate(() => done(new Error('baz')));
},
}),
true,
{
createTransform: () =>
new stream.Transform({
transform(_chunk, _encoding, done) {
done();
},
flush(done) {
setImmediate(() => done(new Error('baz')));
},
}),
hasBug: true,
},
],
[
'transformFrom()',
() =>
// eslint-disable-next-line require-yield
transformFrom(async function*(source) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
for await (const _ of source);
throw new Error('baz');
}),
true,
{
createTransform: () =>
// eslint-disable-next-line require-yield
transformFrom(async function*(source) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
for await (const _ of source);
throw new Error('baz');
}),
hasBug: false,
},
],
];

describe.each(table)('%s', (_, createTransform) => {
describe.each(table)('%s', (_, { createTransform }) => {
it('pipe to WritableStream', async () => {
const resultPromise = promisify(stream.pipeline)(
stream.Readable.from(data),
Expand All @@ -635,8 +641,10 @@ describe('throw error from Transform', () => {
*/
const isBugFixed = Number(/^\d+/.exec(process.versions.node)?.[0]) >= 15;

// eslint-disable-next-line jest/no-identical-title
describe.each(table.filter(([, , isAsync]) => isBugFixed || !isAsync))('%s', (_, createTransform) => {
describe.each(
table.filter(([, { hasBug }]) => isBugFixed || !hasBug),
// eslint-disable-next-line jest/no-identical-title
)('%s', (_, { createTransform }) => {
it('not pipe to WritableStream', async () => {
const resultPromise = promisify(stream.pipeline)(
stream.Readable.from(data),
Expand All @@ -648,8 +656,10 @@ describe('throw error from Transform', () => {
});

if (!isBugFixed) {
// eslint-disable-next-line jest/no-identical-title
describe.each(table.filter(([, , isAsync]) => isAsync))('%s', (_, createTransform) => {
describe.each(
table.filter(([, { hasBug }]) => hasBug),
// eslint-disable-next-line jest/no-identical-title
)('%s', (_, { createTransform }) => {
it('not pipe to WritableStream (error cannot be detected)', async () => {
const transform = createTransform();

Expand Down

0 comments on commit 40406fc

Please sign in to comment.