Skip to content

Commit 7050608

Browse files
XadillaXMylesBorins
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 458b8ab commit 7050608

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

test/common/index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,12 @@ function hijackStdWritable(name, listener) {
803803

804804
stream.writeTimes = 0;
805805
stream.write = function(data, callback) {
806-
listener(data);
806+
try {
807+
listener(data);
808+
} catch (e) {
809+
process.nextTick(() => { throw e; });
810+
}
811+
807812
_write.call(stream, data, callback);
808813
stream.writeTimes++;
809814
};

test/parallel/test-common.js

+23
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)