test: cover Readable.from() sync iterator errors

Signed-off-by: jakecastelli <38635403+jakecastelli@users.noreply.github.com>
PR-URL: https://github.com/nodejs/node/pull/65515
Reviewed-By: Jason Zhang <xzha4350@gmail.com>
Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
This commit is contained in:
jakecastelli authored and Antoine du Hamel committed 2026-09-15 10:49:09 +02:00
1 parent 1e455a4ffe
commit 2342b2bf30
2 files changed
+114 -11

No files matched your search

@@ -101,6 +101,40 @@ async function syncRejectedSupport() {
}
}
async function syncRejectedAfterResolvedSupport() {
const expectedError = new Error('later rejection');
const finallyMustCall = mustCall();
const bodyMustCall = mustCall((chunk) => {
assert.strictEqual(chunk, 'a');
});
const catchMustCall = mustCall((error) => {
assert.strictEqual(error, expectedError);
});
const thirdNextMustNotCall = mustNotCall();
function* generate() {
try {
yield Promise.resolve('a');
yield Promise.reject(expectedError);
thirdNextMustNotCall();
} finally {
finallyMustCall();
}
}
const stream = Readable.from(generate());
try {
for await (const chunk of stream) {
bodyMustCall(chunk);
}
} catch (error) {
catchMustCall(error);
}
assert.strictEqual(stream.destroyed, true);
}
async function noReturnAfterThrow() {
const returnMustNotCall = mustNotCall();
const bodyMustNotCall = mustNotCall();
@@ -187,6 +221,7 @@ Promise.all([
syncSupport(),
syncPromiseSupport(),
syncRejectedSupport(),
syncRejectedAfterResolvedSupport(),
noReturnAfterThrow(),
closeStreamWhileNextIsPending(),
closeAfterNullYielded(),
@@ -1,19 +1,87 @@
'use strict';
const { mustNotCall, expectsError } = require('../common');
const common = require('../common');
const assert = require('assert');
const { Readable } = require('stream');
const { finished } = require('stream/promises');
async function* generate() {
yield null;
}
const stream = Readable.from(generate());
stream.on('error', expectsError({
const expectedError = {
code: 'ERR_STREAM_NULL_VALUES',
name: 'TypeError',
message: 'May not write null values to stream'
}));
};
stream.on('data', mustNotCall());
async function rejectsNull(iterable, expectedChunks = []) {
const stream = Readable.from(iterable);
const chunks = [];
const completion = finished(stream);
stream.on('end', mustNotCall());
stream.on('data', (chunk) => chunks.push(chunk));
await assert.rejects(completion, expectedError);
assert.deepStrictEqual(chunks, expectedChunks);
assert.strictEqual(stream.destroyed, true);
}
async function asyncIteratorYieldsNull() {
const cleanup = common.mustCall();
async function* generate() {
try {
yield null;
} finally {
cleanup();
}
}
await rejectsNull(generate());
}
async function syncIteratorYieldsNull() {
const cleanup = common.mustCall();
function* generate() {
try {
yield null;
} finally {
cleanup();
}
}
await rejectsNull(generate());
}
async function firstSyncValueResolvesToNull() {
const cleanup = common.mustCall();
function* generate() {
try {
yield Promise.resolve(null);
} finally {
cleanup();
}
}
await rejectsNull(generate());
}
async function laterSyncValueResolvesToNull() {
const cleanup = common.mustCall();
function* generate() {
try {
yield Promise.resolve('first');
yield Promise.resolve(null);
} finally {
cleanup();
}
}
await rejectsNull(generate(), ['first']);
}
Promise.all([
asyncIteratorYieldsNull(),
syncIteratorYieldsNull(),
firstSyncValueResolvesToNull(),
laterSyncValueResolvesToNull(),
]).then(common.mustCall());