test: allow leaked global check to be skipped · nodejs/node@57ab3b5 (original) (raw)

2 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -279,34 +279,36 @@ if (global.gc) {
279 279 knownGlobals.push(global.gc);
280 280 }
281 281
282 -if (process.env.NODE_TEST_KNOWN_GLOBALS) {
283 -const knownFromEnv = process.env.NODE_TEST_KNOWN_GLOBALS.split(',');
284 -allowGlobals(...knownFromEnv);
285 -}
286 -
287 282 function allowGlobals(...whitelist) {
288 283 knownGlobals = knownGlobals.concat(whitelist);
289 284 }
290 285
291 -function leakedGlobals() {
292 -const leaked = [];
286 +if (process.env.NODE_TEST_KNOWN_GLOBALS !== '0') {
287 +if (process.env.NODE_TEST_KNOWN_GLOBALS) {
288 +const knownFromEnv = process.env.NODE_TEST_KNOWN_GLOBALS.split(',');
289 +allowGlobals(...knownFromEnv);
290 +}
293 291
294 -for (const val in global) {
295 -if (!knownGlobals.includes(global[val])) {
296 -leaked.push(val);
292 +function leakedGlobals() {
293 +const leaked = [];
294 +
295 +for (const val in global) {
296 +if (!knownGlobals.includes(global[val])) {
297 +leaked.push(val);
298 +}
297 299 }
300 +
301 +return leaked;
298 302 }
299 303
300 -return leaked;
304 +process.on('exit', function() {
305 +const leaked = leakedGlobals();
306 +if (leaked.length > 0) {
307 +assert.fail(`Unexpected global(s) found: ${leaked.join(', ')}`);
308 +}
309 +});
301 310 }
302 311
303 -process.on('exit', function() {
304 -const leaked = leakedGlobals();
305 -if (leaked.length > 0) {
306 -assert.fail(`Unexpected global(s) found: ${leaked.join(', ')}`);
307 -}
308 -});
309 -
310 312 const mustCallChecks = [];
311 313
312 314 function runCallChecks(exitCode) {
Original file line number Diff line number Diff line change
@@ -35,6 +35,17 @@ const { execFile } = require('child_process');
35 35 }));
36 36 }
37 37
38 +// Test for disabling leaked global detection
39 +{
40 +const p = fixtures.path('leakedGlobal.js');
41 +execFile(process.execPath, [p], {
42 +env: { ...process.env, NODE_TEST_KNOWN_GLOBALS: 0 }
43 +}, common.mustCall((err, stdout, stderr) => {
44 +assert.strictEqual(err, null);
45 +assert.strictEqual(stderr.trim(), '');
46 +}));
47 +}
48 +
38 49 // common.mustCall() tests
39 50 assert.throws(function() {
40 51 common.mustCall(function() {}, 'foo');