Browse Source

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 <jasnell@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
canary-base
XadillaX 7 years ago
committed by James M Snell
parent
commit
1ffd01cf7f
  1. 7
      test/common/index.js
  2. 23
      test/parallel/test-common.js

7
test/common/index.js

@ -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++;
};

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

Loading…
Cancel
Save