Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix hijackStdout behavior in console #14647

Closed
wants to merge 3 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion test/common/index.js
Original file line number Diff line number Diff line change
@@ -801,7 +801,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++;
};
22 changes: 22 additions & 0 deletions test/parallel/test-common.js
Original file line number Diff line number Diff line change
@@ -108,3 +108,25 @@ 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) {
Copy link
Contributor

@refack refack Aug 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CR: How is the mustCallAtLeast fit with the ${([ 'err', 'out' ])[uncaughtTimes++]} trick?

Copy link
Contributor Author

@XadillaX XadillaX Aug 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second parameter of common.mustCallAtLeast is 2.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I wasn't clear. I meant it should be mustCall, as any more calls would generate console undefined error

assert.strictEqual(e instanceof Error, true);
assert.strictEqual(
e.message,
`console ${([ 'err', 'out' ])[uncaughtTimes++]} error`);
}, 2));