Skip to content
Closed
Show file tree
Hide file tree
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
Expand Up @@ -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++;
};
Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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));