Skip to content

Commit

Permalink
test: replace more uses of global with globalThis
Browse files Browse the repository at this point in the history
PR-URL: #56712
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
  • Loading branch information
jasnell authored and nodejs-github-bot committed Jan 25, 2025
1 parent 552362a commit 97a3a82
Show file tree
Hide file tree
Showing 74 changed files with 246 additions and 242 deletions.
2 changes: 1 addition & 1 deletion test/parallel/test-aborted-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ test('Aborted with gc cleanup', async () => {
const { promise, resolve } = Promise.withResolvers();

setImmediate(() => {
global.gc();
globalThis.gc();
ac.abort();
strictEqual(ac.signal.aborted, true);
strictEqual(getEventListeners(ac.signal, 'abort').length, 0);
Expand Down
10 changes: 5 additions & 5 deletions test/parallel/test-abortsignal-drop-settled-signals.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function makeSubsequentCalls(limit, done, holdReferences = false) {
// This setImmediate is necessary to ensure that in the last iteration the remaining signal is GCed (if not
// retained)
setImmediate(() => {
global.gc();
globalThis.gc();
done(ac.signal, dependantSymbol);
});
return;
Expand Down Expand Up @@ -50,7 +50,7 @@ function runShortLivedSourceSignal(limit, done) {

function run(iteration) {
if (iteration > limit) {
global.gc();
globalThis.gc();
done(signalRefs);
return;
}
Expand All @@ -74,9 +74,9 @@ function runWithOrphanListeners(limit, done) {
const ac = new AbortController();
if (iteration > limit) {
setImmediate(() => {
global.gc();
globalThis.gc();
setImmediate(() => {
global.gc();
globalThis.gc();

done(composedSignalRefs);
});
Expand Down Expand Up @@ -147,7 +147,7 @@ it('drops settled dependant signals when signal is composite', (t, done) => {
t.assert.strictEqual(controllers[1].signal[kDependantSignals].size, 1);

setImmediate(() => {
global.gc({ execution: 'async' }).then(async () => {
globalThis.gc({ execution: 'async' }).then(async () => {
await gcUntil('all signals are GCed', () => {
const totalDependantSignals = Math.max(
controllers[0].signal[kDependantSignals].size,
Expand Down
8 changes: 4 additions & 4 deletions test/parallel/test-assert-checktag.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ test('', { skip: !hasCrypto }, () => {

{ // At the moment global has its own type tag
const fakeGlobal = {};
Object.setPrototypeOf(fakeGlobal, Object.getPrototypeOf(global));
for (const prop of Object.keys(global)) {
Object.setPrototypeOf(fakeGlobal, Object.getPrototypeOf(globalThis));
for (const prop of Object.keys(globalThis)) {
fakeGlobal[prop] = global[prop];
}
assert.notDeepEqual(fakeGlobal, global);
assert.notDeepEqual(fakeGlobal, globalThis);
// Message will be truncated anyway, don't validate
assert.throws(() => assert.deepStrictEqual(fakeGlobal, global),
assert.throws(() => assert.deepStrictEqual(fakeGlobal, globalThis),
assert.AssertionError);
}

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-async-hooks-destroy-on-gc.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ let asyncId = null;
}

setImmediate(() => {
global.gc();
globalThis.gc();
setImmediate(() => assert.ok(destroyedIds.has(asyncId)));
});
2 changes: 1 addition & 1 deletion test/parallel/test-async-hooks-disable-gc-tracking.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const hook = async_hooks.createHook({
new async_hooks.AsyncResource('foobar', { requireManualDestroy: true });

setImmediate(() => {
global.gc();
globalThis.gc();
setImmediate(() => {
hook.disable();
});
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-async-hooks-prevent-double-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const hook = async_hooks.createHook({
}

setImmediate(() => {
global.gc();
globalThis.gc();
setImmediate(() => {
hook.disable();
});
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-cli-eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ child.exec(
// Regression test for https://github.com/nodejs/node/issues/45336
child.execFile(process.execPath,
['-p',
'Object.defineProperty(global, "fs", { configurable: false });' +
'Object.defineProperty(globalThis, "fs", { configurable: false });' +
'fs === require("node:fs")'],
common.mustSucceed((stdout) => {
assert.match(stdout, /^true/);
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-common-gc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ const { onGC } = require('../common/gc');

{
onGC({}, { ongc: common.mustCall() });
global.gc();
globalThis.gc();
}

{
onGC(process, { ongc: common.mustNotCall() });
global.gc();
globalThis.gc();
}
16 changes: 8 additions & 8 deletions test/parallel/test-console-assign-undefined.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
'use strict';

// Patch global.console before importing modules that may modify the console
// Patch globalThis.console before importing modules that may modify the console
// object.

const tmp = global.console;
global.console = 42;
const tmp = globalThis.console;
globalThis.console = 42;

require('../common');
const assert = require('assert');

// Originally the console had a getter. Test twice to verify it had no side
// effect.
assert.strictEqual(global.console, 42);
assert.strictEqual(global.console, 42);
assert.strictEqual(globalThis.console, 42);
assert.strictEqual(globalThis.console, 42);

assert.throws(
() => console.log('foo'),
{ name: 'TypeError' }
);

global.console = 1;
assert.strictEqual(global.console, 1);
globalThis.console = 1;
assert.strictEqual(globalThis.console, 1);
assert.strictEqual(console, 1);

// Reset the console
global.console = tmp;
globalThis.console = tmp;
console.log('foo');
4 changes: 2 additions & 2 deletions test/parallel/test-console-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ process.stdout.write = process.stderr.write = common.mustNotCall();
// Make sure that the "Console" function exists.
assert.strictEqual(typeof Console, 'function');

assert.strictEqual(requiredConsole, global.console);
assert.strictEqual(requiredConsole, globalThis.console);
// Make sure the custom instanceof of Console works
assert.ok(global.console instanceof Console);
assert.ok(globalThis.console instanceof Console);
assert.ok(!({} instanceof Console));

// Make sure that the Console constructor throws
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-console-self-assign.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
require('../common');

// Assigning to itself should not throw.
global.console = global.console; // eslint-disable-line no-self-assign
globalThis.console = globalThis.console; // eslint-disable-line no-self-assign
2 changes: 1 addition & 1 deletion test/parallel/test-crypto-dh-leak.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const before = process.memoryUsage.rss();
dh.setPrivateKey(privateKey);
}
}
global.gc();
globalThis.gc();
const after = process.memoryUsage.rss();

// RSS should stay the same, ceteris paribus, but allow for
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-domain-crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const crypto = require('crypto');
// Pollution of global is intentional as part of test.
common.allowGlobals(require('domain'));
// See https://github.com/nodejs/node/commit/d1eff9ab
global.domain = require('domain');
globalThis.domain = require('domain');

// Should not throw a 'TypeError: undefined is not a function' exception
crypto.randomBytes(8);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-eventtarget-memoryleakwarning.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ common.expectWarning({
});

await setTimeout(0);
global.gc();
globalThis.gc();
}
})().then(common.mustCall(), common.mustNotCall());
}
2 changes: 1 addition & 1 deletion test/parallel/test-eventtarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ let asyncTest = Promise.resolve();
const et = new EventTarget();
et.addEventListener('foo', common.mustNotCall(), { [kWeakHandler]: {} });
setImmediate(() => {
global.gc();
globalThis.gc();
et.dispatchEvent(new Event('foo'));
});
}
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-finalization-registry-shutdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ process.on('exit', () => {
// This is the final chance to execute JavaScript.
register();
// Queue a FinalizationRegistryCleanupTask by a testing gc request.
global.gc();
globalThis.gc();
});
2 changes: 1 addition & 1 deletion test/parallel/test-fs-filehandle.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ common.expectWarning({
'DeprecationWarning': [[deprecationWarning, 'DEP0137']]
});

global.gc();
globalThis.gc();

setTimeout(() => {}, 10);
2 changes: 1 addition & 1 deletion test/parallel/test-fs-promises-file-handle-close.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ doOpen().then(common.mustCall((fd) => {
})).then(common.mustCall(() => {
setImmediate(() => {
// The FileHandle should be out-of-scope and no longer accessed now.
global.gc();
globalThis.gc();

// Wait an extra event loop turn, as the warning is emitted from the
// native layer in an unref()'ed setImmediate() callback.
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-fs-write-reuse-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ let done = 0;

const ondone = common.mustSucceed(() => {
if (++done < writes) {
if (done % 25 === 0) global.gc();
if (done % 25 === 0) globalThis.gc();
setImmediate(write);
} else {
assert.strictEqual(
Expand Down
8 changes: 6 additions & 2 deletions test/parallel/test-fs-write.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,18 @@ const {
createExternalizableString,
externalizeString,
isOneByteString,
} = global;
} = globalThis;

assert.notStrictEqual(createExternalizableString, undefined);
assert.notStrictEqual(externalizeString, undefined);
assert.notStrictEqual(isOneByteString, undefined);

// Account for extra globals exposed by --expose_externalize_string.
common.allowGlobals(
createExternalizableString,
externalizeString,
isOneByteString,
global.x,
globalThis.x,
);

{
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-gc-http-client-connaborted.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ setImmediate(status);
function status() {
if (done > 0) {
createClients = false;
global.gc();
globalThis.gc();
console.log(`done/collected/total: ${done}/${countGC}/${count}`);
if (countGC === count) {
server.close();
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-gc-net-timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ setImmediate(status);
function status() {
if (done > 0) {
createClients = false;
global.gc();
globalThis.gc();
console.log(`done/collected/total: ${done}/${countGC}/${count}`);
if (countGC === count) {
server.close();
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-gc-tls-external-memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ connect();

function connect() {
if (runs % 64 === 0)
global.gc();
globalThis.gc();
const externalMemoryUsage = process.memoryUsage().external;
assert(externalMemoryUsage >= 0, `${externalMemoryUsage} < 0`);
if (runs++ === 512) {
Expand Down
12 changes: 6 additions & 6 deletions test/parallel/test-global-setters.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ assert.strictEqual(process, _process);
// eslint-disable-next-line no-global-assign
process = 'asdf';
assert.strictEqual(process, 'asdf');
assert.strictEqual(global.process, 'asdf');
global.process = _process;
assert.strictEqual(globalThis.process, 'asdf');
globalThis.process = _process;
assert.strictEqual(process, _process);
assert.strictEqual(
typeof Object.getOwnPropertyDescriptor(global, 'process').get,
typeof Object.getOwnPropertyDescriptor(globalThis, 'process').get,
'function');

assert.strictEqual(Buffer, _Buffer);
// eslint-disable-next-line no-global-assign
Buffer = 'asdf';
assert.strictEqual(Buffer, 'asdf');
assert.strictEqual(global.Buffer, 'asdf');
global.Buffer = _Buffer;
assert.strictEqual(globalThis.Buffer, 'asdf');
globalThis.Buffer = _Buffer;
assert.strictEqual(Buffer, _Buffer);
assert.strictEqual(
typeof Object.getOwnPropertyDescriptor(global, 'Buffer').get,
typeof Object.getOwnPropertyDescriptor(globalThis, 'Buffer').get,
'function');
14 changes: 7 additions & 7 deletions test/parallel/test-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ for (const moduleName of builtinModules) {
'crypto',
'navigator',
];
assert.deepStrictEqual(new Set(Object.keys(global)), new Set(expected));
assert.deepStrictEqual(new Set(Object.keys(globalThis)), new Set(expected));
expected.forEach((value) => {
const desc = Object.getOwnPropertyDescriptor(global, value);
const desc = Object.getOwnPropertyDescriptor(globalThis, value);
if (typeof desc.value === 'function') {
assert.strictEqual(desc.value.name, value);
} else if (typeof desc.get === 'function') {
Expand All @@ -74,15 +74,15 @@ for (const moduleName of builtinModules) {
common.allowGlobals('bar', 'foo');

baseFoo = 'foo'; // eslint-disable-line no-undef
global.baseBar = 'bar';
globalThis.baseBar = 'bar';

assert.strictEqual(global.baseFoo, 'foo',
`x -> global.x failed: global.baseFoo = ${global.baseFoo}`);
assert.strictEqual(globalThis.baseFoo, 'foo',
`x -> globalThis.x failed: globalThis.baseFoo = ${globalThis.baseFoo}`);

assert.strictEqual(baseBar, // eslint-disable-line no-undef
'bar',
// eslint-disable-next-line no-undef
`global.x -> x failed: baseBar = ${baseBar}`);
`globalThis.x -> x failed: baseBar = ${baseBar}`);

const mod = require(fixtures.path('global', 'plain'));
const fooBar = mod.fooBar;
Expand All @@ -91,4 +91,4 @@ assert.strictEqual(fooBar.foo, 'foo');

assert.strictEqual(fooBar.bar, 'bar');

assert.strictEqual(Object.prototype.toString.call(global), '[object global]');
assert.strictEqual(Object.prototype.toString.call(globalThis), '[object global]');
4 changes: 2 additions & 2 deletions test/parallel/test-h2leak-destroy-session-on-socket-ended.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ server.on('secureConnection', (s) => {
firstServerStream = null;

setImmediate(() => {
global.gc();
global.gc();
globalThis.gc();
globalThis.gc();

server.close();
});
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-heapdump-async-hooks-init-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ async_hooks.createHook({


Promise.resolve().then(() => {});
setImmediate(global.gc);
setImmediate(globalThis.gc);
2 changes: 1 addition & 1 deletion test/parallel/test-http-agent-domain-reused-gc.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async_hooks.createHook({
},
before(id) {
if (id === reusedHandleId) {
global.gc();
globalThis.gc();
checkBeforeCalled();
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http-parser-bad-ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let messagesComplete = 0;

function flushPool() {
Buffer.allocUnsafe(Buffer.poolSize - 1);
global.gc();
globalThis.gc();
}

function demoBug(part1, part2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ for (let i = 0; i < max; i++) {
}

setImmediate(() => {
global.gc();
globalThis.gc();
});
Loading

0 comments on commit 97a3a82

Please sign in to comment.