diff --git a/test/common/index.js b/test/common/index.js index a5ca4cec576e74..acade1d858152c 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -801,7 +801,12 @@ function hijackStdWritable(name, listener) { stream.writeTimes = 0; stream.write = function(data, callback) { - listener(data); + try { + listener(data); + } catch (e) { + process.nextTick(() => { throw e; }); + } + _write.call(stream, data, callback); stream.writeTimes++; }; diff --git a/test/parallel/test-common.js b/test/parallel/test-common.js index 7fee3e0b841437..647c39d939f2aa 100644 --- a/test/parallel/test-common.js +++ b/test/parallel/test-common.js @@ -108,3 +108,26 @@ const HIJACK_TEST_ARRAY = [ 'foo\n', 'bar\n', 'baz\n' ]; common[`restoreStd${txt}`](); assert.strictEqual(originalWrite, stream.write); }); + +// hijackStderr and hijackStdout again +// for console +[[ 'err', 'error' ], [ 'out', 'log' ]].forEach(([ type, method ]) => { + common[`hijackStd${type}`](common.mustCall(function(data) { + assert.strictEqual(data, 'test\n'); + + // throw an error + throw new Error(`console ${type} error`); + })); + + console[method]('test'); + common[`restoreStd${type}`](); +}); + +let uncaughtTimes = 0; +process.on('uncaughtException', common.mustCallAtLeast(function(e) { + assert.strictEqual(uncaughtTimes < 2, true); + assert.strictEqual(e instanceof Error, true); + assert.strictEqual( + e.message, + `console ${([ 'err', 'out' ])[uncaughtTimes++]} error`); +}, 2));