Skip to content

Commit

Permalink
test: add test for error case of iterator (#1000)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxmvshnvsk authored Dec 17, 2024
1 parent 7566081 commit f65342e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,8 @@ export class ProcessPromise extends Promise<ProcessOutput> {
}

if (last) yield last

if ((await this.exitCode) !== 0) throw this._output
}

// Stream-like API
Expand Down
26 changes: 26 additions & 0 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,32 @@ describe('core', () => {
assert.equal(chunks[0], 'Chunk1', 'First chunk should be "Chunk1"')
assert.equal(chunks[3], 'Chunk4', 'Second chunk should be "Chunk4"')
})

it('should process all output before handling a non-zero exit code', async () => {
const process = $`sleep 0.1; echo foo; sleep 0.1; echo bar; sleep 0.1; exit 1;`

const chunks = []

let errorCaught = null
try {
for await (const chunk of process) {
chunks.push(chunk)
}
} catch (err) {
errorCaught = err
}

assert.equal(chunks.length, 2, 'Should have received 2 chunks')
assert.equal(chunks[0], 'foo', 'First chunk should be "foo"')
assert.equal(chunks[1], 'bar', 'Second chunk should be "bar"')

assert.ok(errorCaught, 'An error should have been caught')
assert.equal(
errorCaught.exitCode,
1,
'The process exit code should be 1'
)
})
})

test('quiet() mode is working', async () => {
Expand Down

0 comments on commit f65342e

Please sign in to comment.