Skip to content

Commit

Permalink
Fail with the correct error when stream.write or stream.end throws.
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed May 29, 2016
1 parent 8f9724d commit dc19f1f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lib/unexpectedStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
});
});

Expand Down
13 changes: 13 additions & 0 deletions test/unexpectedStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});

0 comments on commit dc19f1f

Please sign in to comment.