Skip to content

Commit

Permalink
test: fix hijackStdout behavior in console
Browse files Browse the repository at this point in the history
`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: nodejs#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]>
  • Loading branch information
XadillaX authored and jasnell committed Aug 29, 2017
1 parent 5c0d64e commit 1ffd01c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
7 changes: 6 additions & 1 deletion test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,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++;
};
Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));

0 comments on commit 1ffd01c

Please sign in to comment.