Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export const waitForEvent = (
export const combineStreams = (streams: Readable[]): Readable => {
let streamCount = streams.length;
const combined = new PassThrough();
if (streamCount === 0) {
combined.end();
}
const maybeEmitEnd = () => {
if (--streamCount === 0) {
combined.end();
Expand Down
26 changes: 26 additions & 0 deletions src/test/main_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,32 @@ describe.for(variants)('exec ($name)', ({x, isAsync}) => {
});

describe('exec (async)', () => {
test.each(['ignore', 'inherit'] as const)(
'async iteration completes with stdio=%s',
async (stdio) => {
const proc = x(process.execPath, ['-e', ''], {
nodeOptions: {stdio}
});
const lines = [];
for await (const line of proc) lines.push(line);
expect(lines).toEqual([]);
expect(proc.exitCode).toBe(0);
},
2000
);

test('async iteration reports a nonzero exit without output streams', async () => {
const proc = x(process.execPath, ['-e', 'process.exit(1)'], {
nodeOptions: {stdio: 'ignore'},
throwOnError: true
});
await expect(async () => {
for await (const _line of proc) {
// no output streams
}
}).rejects.toThrow(NonZeroExitError);
}, 2000);

test('non-zero exitCode throws when throwOnError=true', async () => {
const proc = x('node', ['-e', 'process.exit(1);'], {throwOnError: true});
try {
Expand Down
8 changes: 8 additions & 0 deletions src/test/stream_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ describe('waitForEvent', async () => {
});

describe('combineStreams', async () => {
test('ends without input streams', async () => {
const chunks = [];
for await (const chunk of combineStreams([])) {
chunks.push(chunk);
}
expect(chunks).toEqual([]);
}, 1000);

test('works with a single stream', async () => {
const stream = Readable.from(['foo', 'bar']);
const combined = combineStreams([stream]);
Expand Down