diff --git a/src/core.ts b/src/core.ts index 090a610d75..f7dbd80c75 100644 --- a/src/core.ts +++ b/src/core.ts @@ -472,6 +472,11 @@ export class ProcessPromise extends Promise { } this._piped = true if (dest instanceof ProcessPromise) { + const _reject = this._reject + this._reject = function (v) { + _reject(v) + dest._reject(v) + } dest.stdio('pipe') dest._prerun = this.run.bind(this) dest._postrun = () => { diff --git a/test/core.test.js b/test/core.test.js index 4380985b24..d1a46cd4cc 100644 --- a/test/core.test.js +++ b/test/core.test.js @@ -324,6 +324,27 @@ describe('core', () => { assert.equal(stdout, 'HELLO WORLD\n') }) + test('propagates rejection', async () => { + const p1 = $`exit 1` + const p2 = p1.pipe($`echo hello`) + + try { + await p1 + } catch (e) { + assert.equal(e.exitCode, 1) + } + + try { + await p2 + } catch (e) { + assert.equal(e.exitCode, 1) + } + + const p3 = await $({ nothrow: true })`echo hello && exit 1`.pipe($`cat`) + assert.equal(p3.exitCode, 0) + assert.equal(p3.stdout.trim(), 'hello') + }) + test('accepts Writable', async () => { let contents = '' let stream = new Writable({