From dc19f1f96484f8ad407b9e4a7fcfc26c27b42121 Mon Sep 17 00:00:00 2001 From: Andreas Lind Date: Sun, 29 May 2016 23:37:32 +0200 Subject: [PATCH] Fail with the correct error when stream.write or stream.end throws. --- lib/unexpectedStream.js | 17 ++++++++++------- test/unexpectedStream.js | 13 +++++++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/lib/unexpectedStream.js b/lib/unexpectedStream.js index ab57c4d..47f6b66 100644 --- a/lib/unexpectedStream.js +++ b/lib/unexpectedStream.js @@ -26,7 +26,7 @@ module.exports = { subject.on('error', reject); } - var returnValue = resolve(expect.shift(pipeThroughStreams[pipeThroughStreams.length - 1])); + var returnValue = expect.shift(pipeThroughStreams[pipeThroughStreams.length - 1]); pipeThroughStreams.forEach(function (pipeThroughStream) { pipeThroughStream.on('error', reject); @@ -35,13 +35,16 @@ module.exports = { if (!subjectType.is('Stream')) { // string, Buffer, or array - (Array.isArray(subject) ? subject : [ subject ]).forEach(function (chunk) { - pipeThroughStreams[0].write(chunk); - }); - pipeThroughStreams[0].end(); + try { + (Array.isArray(subject) ? subject : [ subject ]).forEach(function (chunk) { + pipeThroughStreams[0].write(chunk); + }); + pipeThroughStreams[0].end(); + } catch (e) { + return reject(e); + } } - - return returnValue; + return resolve(returnValue); }); }); diff --git a/test/unexpectedStream.js b/test/unexpectedStream.js index 438d584..be84c8c 100644 --- a/test/unexpectedStream.js +++ b/test/unexpectedStream.js @@ -228,4 +228,17 @@ describe('unexpected-stream', function () { }); }); }); + + describe('with a broken stream that throws from the write method', function () { + it('should propagate the error', function () { + var stream = new EventEmitter(); + stream.readable = stream.writable = true; + stream.write = function () { + throw new Error('ugh'); + }; + return expect(function () { + return expect(['abc'], 'when piped through', stream, 'to yield output satisfying', 'blabla'); + }, 'to error', 'ugh'); + }); + }); });