1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22// This test cannot run in strict mode because it tests that `baseFoo` is 23// treated as a global without being declared with `var`/`let`/`const`. 24 25/* eslint-disable strict */ 26const common = require('../common'); 27const fixtures = require('../common/fixtures'); 28 29const assert = require('assert'); 30const { builtinModules } = require('module'); 31 32// Load all modules to actually cover most code parts. 33builtinModules.forEach((moduleName) => { 34 if (!moduleName.includes('/')) { 35 try { 36 // This could throw for e.g., crypto if the binary is not compiled 37 // accordingly. 38 require(moduleName); 39 } catch {} 40 } 41}); 42 43{ 44 const expected = [ 45 'global', 46 'queueMicrotask', 47 'clearImmediate', 48 'clearInterval', 49 'clearTimeout', 50 'setImmediate', 51 'setInterval', 52 'setTimeout', 53 ]; 54 assert.deepStrictEqual(new Set(Object.keys(global)), new Set(expected)); 55} 56 57common.allowGlobals('bar', 'foo'); 58 59baseFoo = 'foo'; // eslint-disable-line no-undef 60global.baseBar = 'bar'; 61 62assert.strictEqual(global.baseFoo, 'foo', 63 `x -> global.x failed: global.baseFoo = ${global.baseFoo}`); 64 65assert.strictEqual(baseBar, // eslint-disable-line no-undef 66 'bar', 67 // eslint-disable-next-line no-undef 68 `global.x -> x failed: baseBar = ${baseBar}`); 69 70const mod = require(fixtures.path('global', 'plain')); 71const fooBar = mod.fooBar; 72 73assert.strictEqual(fooBar.foo, 'foo'); 74 75assert.strictEqual(fooBar.bar, 'bar'); 76 77assert.strictEqual(Object.prototype.toString.call(global), '[object global]'); 78