diff --git a/source/iterable.js b/source/iterable.js index f8762cb..383da5c 100644 --- a/source/iterable.js +++ b/source/iterable.js @@ -7,13 +7,14 @@ export const lineIterator = async function * (resultPromise, {state}, streamName } state.isIterating = true; - const instance = await resultPromise.nodeChildProcess; - const stream = instance[streamName]; - if (!stream) { - return; - } try { + const instance = await resultPromise.nodeChildProcess; + const stream = instance[streamName]; + if (!stream) { + return; + } + let buffer = ''; for await (const chunk of stream.iterator({destroyOnReturn: false})) { const lines = `${buffer}${chunk}`.split(/\r?\n/); diff --git a/test/index.js b/test/index.js index a0f3457..747f050 100644 --- a/test/index.js +++ b/test/index.js @@ -53,7 +53,12 @@ const nodePrintStderr = nodeEval(`console.error("${testString}")`); const nodePrintBoth = nodeEval(`console.log("${testString}"); setTimeout(() => { console.error("${secondTestString}"); -}, 0)`); +}, 0);`); +const nodePrintBothFail = nodeEval(`console.log("${testString}"); +setTimeout(() => { + console.error("${secondTestString}"); + process.exit(2); +}, 0);`); const nodePrintFail = nodeEval(`console.log("${testString}"); process.exit(2);`); const nodePrintSleep = nodeEval(`setTimeout(() => { @@ -483,6 +488,17 @@ test('promise[Symbol.asyncIterator] has no iterations if only options.stdout + o t.is(output, ''); }); +test('promise.stdout has no iterations but waits for the subprocess if options.stdout "ignore"', async t => { + const promise = nanoSpawn(...nodePrintBothFail, {stdout: 'ignore'}); + const error = await t.throwsAsync(arrayFromAsync(promise.stdout)); + assertFail(t, error); + const promiseError = await t.throwsAsync(promise); + t.is(promiseError, error); + t.is(promiseError.stdout, ''); + t.is(promiseError.stderr, ''); + t.is(promiseError.output, ''); +}); + test('result.stdout is an empty string if options.stdout "ignore"', async t => { const {stdout, stderr, output} = await nanoSpawn(...nodePrintBoth, {stdout: 'ignore'}); t.is(stdout, '');