Skip to content

Commit 1ffd01c

Browse files
XadillaXjasnell
authored andcommitted
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: #14647 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent 5c0d64e commit 1ffd01c

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

test/common/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,12 @@ function hijackStdWritable(name, listener) {
809809

810810
stream.writeTimes = 0;
811811
stream.write = function(data, callback) {
812-
listener(data);
812+
try {
813+
listener(data);
814+
} catch (e) {
815+
process.nextTick(() => { throw e; });
816+
}
817+
813818
_write.call(stream, data, callback);
814819
stream.writeTimes++;
815820
};

test/parallel/test-common.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,26 @@ const HIJACK_TEST_ARRAY = [ 'foo\n', 'bar\n', 'baz\n' ];
108108
common[`restoreStd${txt}`]();
109109
assert.strictEqual(originalWrite, stream.write);
110110
});
111+
112+
// hijackStderr and hijackStdout again
113+
// for console
114+
[[ 'err', 'error' ], [ 'out', 'log' ]].forEach(([ type, method ]) => {
115+
common[`hijackStd${type}`](common.mustCall(function(data) {
116+
assert.strictEqual(data, 'test\n');
117+
118+
// throw an error
119+
throw new Error(`console ${type} error`);
120+
}));
121+
122+
console[method]('test');
123+
common[`restoreStd${type}`]();
124+
});
125+
126+
let uncaughtTimes = 0;
127+
process.on('uncaughtException', common.mustCallAtLeast(function(e) {
128+
assert.strictEqual(uncaughtTimes < 2, true);
129+
assert.strictEqual(e instanceof Error, true);
130+
assert.strictEqual(
131+
e.message,
132+
`console ${([ 'err', 'out' ])[uncaughtTimes++]} error`);
133+
}, 2));

0 commit comments

Comments
 (0)