From 7050608593cee4377aa57debb8010c1b8839ec86 Mon Sep 17 00:00:00 2001 From: XadillaX Date: Sun, 6 Aug 2017 14:16:05 +0800 Subject: [PATCH] test: fix hijackStdout behavior in console `console.log` and some other function will swallow the exception in `stdout.write`. So an asynchronous exception is needed, or `common.hijackStdout` won't detect some exception. Refs: https://github.com/nodejs/node/blob/v8.2.1/lib/console.js#L87 PR-URL: https://github.com/nodejs/node/pull/14647 Reviewed-By: James M Snell Reviewed-By: Refael Ackermann Reviewed-By: Yuta Hiroto Reviewed-By: Ruben Bridgewater Reviewed-By: Joyee Cheung --- test/common/index.js | 7 ++++++- test/parallel/test-common.js | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/test/common/index.js b/test/common/index.js index b276b8ff1a9a3b..f4631f4a0b14fd 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -803,7 +803,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 819cd815538b43..50832b3343cf86 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));