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

console: make console more standard compliant #17708

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
6 changes: 2 additions & 4 deletions lib/internal/bootstrap_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,8 @@
const wrappedConsole = NativeModule.require('console');
Object.defineProperty(global, 'console', {
configurable: true,
enumerable: true,
get() {
return wrappedConsole;
}
enumerable: false,
value: wrappedConsole
});
setupInspector(originalConsole, wrappedConsole, Module);
}
Expand Down
2 changes: 1 addition & 1 deletion test/message/console_low_stack_space.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function a() {
try {
return a();
} catch (e) {
compiledConsole = consoleDescriptor.get();
compiledConsole = consoleDescriptor.value;
if (compiledConsole.log) {
// Using `console.log` itself might not succeed yet, but the code for it
// has been compiled.
Expand Down
31 changes: 31 additions & 0 deletions test/parallel/test-console-assign-undefined.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

// Patch global.console before importing modules that may modify the console
// object.

const tmp = global.console;
global.console = 42;

const common = require('../common');
const assert = require('assert');

// Originally the console had a getter. Test twice to verify it had no side
// effect.
assert.strictEqual(global.console, 42);
assert.strictEqual(global.console, 42);

common.expectsError(
() => console.log('foo'),
{
type: TypeError,
message: 'console.log is not a function'
}
);

global.console = 1;
assert.strictEqual(global.console, 1);
assert.strictEqual(console, 1);

// Reset the console
global.console = tmp;
console.log('foo');
52 changes: 52 additions & 0 deletions test/parallel/test-console-is-a-namespace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

require('../common');

const assert = require('assert');
const { test, assert_equals, assert_true, assert_false } =
require('../common/wpt');

assert.doesNotThrow(() => {
global.console = global.console;
});

const self = global;

/* eslint-disable quotes, max-len */

/* The following tests should not be modified as they are copied */
/* WPT Refs:
https://github.com/w3c/web-platform-tests/blob/40e451c/console/console-is-a-namespace.any.js
License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
*/

// https://heycam.github.io/webidl/#es-namespaces
// https://console.spec.whatwg.org/#console-namespace

test(() => {
assert_true(self.hasOwnProperty("console"));
}, "console exists on the global object");

test(() => {
const propDesc = Object.getOwnPropertyDescriptor(self, "console");
assert_equals(propDesc.writable, true, "must be writable");
assert_equals(propDesc.enumerable, false, "must not be enumerable");
assert_equals(propDesc.configurable, true, "must be configurable");
assert_equals(propDesc.value, console, "must have the right value");
}, "console has the right property descriptors");

test(() => {
assert_false("Console" in self);
}, "Console (uppercase, as if it were an interface) must not exist");

test(() => {
const prototype1 = Object.getPrototypeOf(console);
const prototype2 = Object.getPrototypeOf(prototype1);

// This got commented out from the original test because in Node.js all
// functions are declared on the prototype.
// assert_equals(Object.getOwnPropertyNames(prototype1).length, 0, "The [[Prototype]] must have no properties");
assert_equals(prototype2, Object.prototype, "The [[Prototype]]'s [[Prototype]] must be %ObjectPrototype%");
}, "The prototype chain must be correct");

/* eslint-enable */