Skip to content

Commit 7809f38

Browse files
committed
test: improve console tests
PR-URL: #17708 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
1 parent 6ff52b6 commit 7809f38

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed
Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
11
'use strict';
22

3-
// Should be above require, because code in require read console
4-
// what we are trying to avoid
5-
// set should be earlier than get
3+
// Patch global.console before importing modules that may modify the console
4+
// object.
65

7-
global.console = undefined;
6+
const tmp = global.console;
7+
global.console = 42;
88

9-
// Initially, the `console` variable is `undefined`, since console will be
10-
// lazily loaded in the getter.
11-
12-
require('../common');
9+
const common = require('../common');
1310
const assert = require('assert');
1411

15-
// global.console's getter is called
16-
// Since the `console` cache variable is `undefined` and therefore false-y,
17-
// the getter still calls NativeModule.require() and returns the object
18-
// obtained from it, instead of returning `undefined` as expected.
12+
// Originally the console had a getter. Test twice to verify it had no side
13+
// effect.
14+
assert.strictEqual(global.console, 42);
15+
assert.strictEqual(global.console, 42);
1916

20-
assert.strictEqual(global.console, undefined, 'first read');
21-
assert.strictEqual(global.console, undefined, 'second read');
17+
common.expectsError(
18+
() => console.log('foo'),
19+
{
20+
type: TypeError,
21+
message: 'console.log is not a function'
22+
}
23+
);
2224

2325
global.console = 1;
24-
assert.strictEqual(global.console, 1, 'set true-like primitive');
26+
assert.strictEqual(global.console, 1);
27+
assert.strictEqual(console, 1);
2528

26-
global.console = 0;
27-
assert.strictEqual(global.console, 0, 'set false-like primitive, again');
29+
// Reset the console
30+
global.console = tmp;
31+
console.log('foo');

test/parallel/test-console-is-a-namespace.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ assert.doesNotThrow(() => {
1212

1313
const self = global;
1414

15-
/* eslint-disable */
16-
/* The following tests are copied from */
15+
/* eslint-disable quotes, max-len */
16+
17+
/* The following tests should not be modified as they are copied */
1718
/* WPT Refs:
1819
https://github.com/w3c/web-platform-tests/blob/40e451c/console/console-is-a-namespace.any.js
1920
License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
@@ -38,12 +39,14 @@ test(() => {
3839
assert_false("Console" in self);
3940
}, "Console (uppercase, as if it were an interface) must not exist");
4041

42+
test(() => {
43+
const prototype1 = Object.getPrototypeOf(console);
44+
const prototype2 = Object.getPrototypeOf(prototype1);
4145

42-
// test(() => {
43-
// const prototype1 = Object.getPrototypeOf(console);
44-
// const prototype2 = Object.getPrototypeOf(prototype1);
46+
// This got commented out from the original test because in Node.js all
47+
// functions are declared on the prototype.
48+
// assert_equals(Object.getOwnPropertyNames(prototype1).length, 0, "The [[Prototype]] must have no properties");
49+
assert_equals(prototype2, Object.prototype, "The [[Prototype]]'s [[Prototype]] must be %ObjectPrototype%");
50+
}, "The prototype chain must be correct");
4551

46-
// assert_equals(Object.getOwnPropertyNames(prototype1).length, 0, "The [[Prototype]] must have no properties");
47-
// assert_equals(prototype2, Object.prototype, "The [[Prototype]]'s [[Prototype]] must be %ObjectPrototype%");
48-
// }, "The prototype chain must be correct");
4952
/* eslint-enable */

0 commit comments

Comments
 (0)