From ea237b22fd7aeeb29eaa2398df623c5c70b82fc5 Mon Sep 17 00:00:00 2001 From: wandalen Date: Mon, 17 Apr 2017 03:58:01 +0300 Subject: [PATCH] console: make console consistent with the standard - overridable make console overridable by custom console( without delete ) add test suite from w3c/web-platform-tests( console-is-a-namespace ) few test cases commented out from the test suite to make nodejs console even more consistent further changes needed which will come in the next commit which could be more breaking than this one Fixes: https://github.com/nodejs/node/issues/11805 --- lib/internal/bootstrap_node.js | 8 ++++ test/parallel/test-console-is-a-namespace.js | 44 ++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 test/parallel/test-console-is-a-namespace.js diff --git a/lib/internal/bootstrap_node.js b/lib/internal/bootstrap_node.js index a576eeb5e579f6..0406ed67fc69e5 100644 --- a/lib/internal/bootstrap_node.js +++ b/lib/internal/bootstrap_node.js @@ -277,7 +277,15 @@ NativeModule.require('console') : installInspectorConsole(originalConsole); } + // Object.defineProperty(global, 'console', { + // configurable: true, + // writable: true, + // value: console + // }); return console; + }, + set: function(customConsole) { + console = customConsole; } }); setupInspectorCommandLineAPI(); diff --git a/test/parallel/test-console-is-a-namespace.js b/test/parallel/test-console-is-a-namespace.js new file mode 100644 index 00000000000000..25749980253559 --- /dev/null +++ b/test/parallel/test-console-is-a-namespace.js @@ -0,0 +1,44 @@ +'use strict'; +// https://heycam.github.io/webidl/#es-namespaces +// https://console.spec.whatwg.org/#console-namespace +// https://github.com/w3c/web-platform-tests/blob/master/console/console-is-a-namespace.any.js + +const common = require('../common'); +const assert = require('assert'); +const { test, assert_equals, assert_true, assert_false } = common.WPT; + +assert.doesNotThrow(() => { + global.console = global.console; +}); + +test(() => { + assert_true(global.hasOwnProperty('console')); +}, 'console exists on the global object'); + +test(() => { + assert_true(global.hasOwnProperty('console')); +}, 'console exists on the global object'); + +test(() => { + const propDesc = Object.getOwnPropertyDescriptor(global, '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 global); +}, 'Console (uppercase, as if it were an interface) must not exist'); + +test(() => { + const prototype1 = Object.getPrototypeOf(console); + const prototype2 = Object.getPrototypeOf(prototype1); + // console.log('console',Object.getOwnPropertyNames(console)); + // console.log('prototype1',Object.getOwnPropertyNames(prototype1)); + // assert_equals(Object.getOwnPropertyNames(prototype1).length, 0, + // 'The [[Prototype]] must have no properties'); + assert_equals(prototype2, Object.prototype, + 'The [[Prototype]]\'s [[Prototype]] must be Object Prototype'); + +}, 'The prototype chain must be correct');