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

events: make sure console functions exist #4479

Closed
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ EventEmitter.prototype.addListener = function addListener(type, listener) {
'leak detected. %d %s listeners added. ' +
'Use emitter.setMaxListeners() to increase limit.',
existing.length, type);
console.trace();
if (console.trace)
console.trace();
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ exports.error = function(msg) {
args[0] = fmt;
for (let i = 1; i < arguments.length; ++i)
args[i] = arguments[i];
console.error.apply(console, args);
if (console.error)
console.error.apply(console, args);
} else {
console.error(fmt);
}
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-global-console-exists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const assert = require('assert');
const events = require('events');

const old_default = events.defaultMaxListeners;
events.defaultMaxListeners = 1;

const e = new events.EventEmitter();
Copy link
Contributor

Choose a reason for hiding this comment

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

Redundant EventEmitter

e.on('hello', function() {});

assert.ok(!e._events['hello'].hasOwnProperty('warned'));
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps it would be better to use a public API to check, such as:

assert.ok(!e.listeners('hello')[0].hasOwnProperty('warned'));

Copy link
Author

Choose a reason for hiding this comment

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

The listeners() API leaves out the warned property: https://github.com/nodejs/node/blob/master/lib/events.js#L393.

Copy link
Member

Choose a reason for hiding this comment

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

While I agree that it's the most efficient approach, using an internal API in a test is a bit troublesome. The "correct" way to do this would likely be to direct the stderr/stdout output to a stream and apply a regex to ensure that the result is as the user should expect it to be. It's a bit of a workaround but it ensures that we're testing exactly what the user would see.


e.on('hello', function() {});

assert.ok(e._events['hello'].hasOwnProperty('warned'));

events.defaultMaxListeners = old_default;

// this caches console, so place it after the test just to appease the linter
require('../common');
Copy link
Contributor

Choose a reason for hiding this comment

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

A better solution would be to put a /* eslint-disable required-modules */ on top of this file, along with a comment.