Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,34 +277,36 @@ if (global.gc) {
knownGlobals.push(global.gc);
}

if (process.env.NODE_TEST_KNOWN_GLOBALS) {
const knownFromEnv = process.env.NODE_TEST_KNOWN_GLOBALS.split(',');
allowGlobals(...knownFromEnv);
}

function allowGlobals(...whitelist) {
knownGlobals = knownGlobals.concat(whitelist);
}

function leakedGlobals() {
const leaked = [];
if (process.env.NODE_TEST_KNOWN_GLOBALS !== '0') {
if (process.env.NODE_TEST_KNOWN_GLOBALS) {
const knownFromEnv = process.env.NODE_TEST_KNOWN_GLOBALS.split(',');
allowGlobals(...knownFromEnv);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be something like:

allowGlobals(...knownFromEnv.map((name) => globalThis[name]));

}

for (const val in global) {
if (!knownGlobals.includes(global[val])) {
leaked.push(val);
function leakedGlobals() {
const leaked = [];

for (const val in global) {
if (!knownGlobals.includes(global[val])) {
leaked.push(val);
}
}

return leaked;
}

return leaked;
process.on('exit', function() {
const leaked = leakedGlobals();
if (leaked.length > 0) {
assert.fail(`Unexpected global(s) found: ${leaked.join(', ')}`);
}
});
}

process.on('exit', function() {
const leaked = leakedGlobals();
if (leaked.length > 0) {
assert.fail(`Unexpected global(s) found: ${leaked.join(', ')}`);
}
});

const mustCallChecks = [];

function runCallChecks(exitCode) {
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ const { execFile } = require('child_process');
}));
}

// Test for disabling leaked global detection
{
const p = fixtures.path('leakedGlobal.js');
execFile(process.execPath, [p], {
env: { ...process.env, NODE_TEST_KNOWN_GLOBALS: 0 }
}, common.mustCall((err, stdout, stderr) => {
assert.strictEqual(err, null);
assert.strictEqual(stderr.trim(), '');
}));
}

// common.mustCall() tests
assert.throws(function() {
common.mustCall(function() {}, 'foo');
Expand Down