|
1 | 1 | 'use strict';
|
2 | 2 |
|
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. |
6 | 5 |
|
7 |
| -global.console = undefined; |
| 6 | +const tmp = global.console; |
| 7 | +global.console = 42; |
8 | 8 |
|
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'); |
13 | 10 | const assert = require('assert');
|
14 | 11 |
|
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); |
19 | 16 |
|
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 | +); |
22 | 24 |
|
23 | 25 | global.console = 1;
|
24 |
| -assert.strictEqual(global.console, 1, 'set true-like primitive'); |
| 26 | +assert.strictEqual(global.console, 1); |
| 27 | +assert.strictEqual(console, 1); |
25 | 28 |
|
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'); |
0 commit comments