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      // Continue regardless of error.
41    }
42  }
43});
44
45{
46  const expected = [
47    'global',
48    'queueMicrotask',
49    'clearImmediate',
50    'clearInterval',
51    'clearTimeout',
52    'atob',
53    'btoa',
54    'performance',
55    'setImmediate',
56    'setInterval',
57    'setTimeout',
58    'structuredClone',
59    'fetch',
60  ];
61  assert.deepStrictEqual(new Set(Object.keys(global)), new Set(expected));
62}
63
64common.allowGlobals('bar', 'foo');
65
66baseFoo = 'foo'; // eslint-disable-line no-undef
67global.baseBar = 'bar';
68
69assert.strictEqual(global.baseFoo, 'foo',
70                   `x -> global.x failed: global.baseFoo = ${global.baseFoo}`);
71
72assert.strictEqual(baseBar, // eslint-disable-line no-undef
73                   'bar',
74                   // eslint-disable-next-line no-undef
75                   `global.x -> x failed: baseBar = ${baseBar}`);
76
77const mod = require(fixtures.path('global', 'plain'));
78const fooBar = mod.fooBar;
79
80assert.strictEqual(fooBar.foo, 'foo');
81
82assert.strictEqual(fooBar.bar, 'bar');
83
84assert.strictEqual(Object.prototype.toString.call(global), '[object global]');
85