Skip to content

Commit 0351622

Browse files
test: release each test file's jest environment when it finishes (#22729)
* test: release each test file's jest environment when it finishes Two harness patches outlived the file that made them, so a jest worker kept every finished file's environment alive: deprecationTracking wrapped the shared util.deprecate on top of the previous file's wrapper, and the runner's vm.runInThisContext left expect and it on the worker's own global. Jest 27 never recycles workers on Node 16, so the heap neared its limit and the last TestCases build crawled through back-to-back full GCs past the 60s timeout. * test: clear globals a bundle defines that can't be deleted A top-level var or function can't be deleted from the worker's global, and strict-mode delete threw, failing the file. Clear it instead, skip what can't be changed, and read the global with `this`, which Node 10 has.
1 parent 2f4b6be commit 0351622

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

‎test/harness/setupTestFramework.js‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use strict";
22

3+
const vm = require("vm");
34
const { toMatchSnapshot } = require("jest-snapshot");
45
const { getActiveSnapshotState } = require("./snapshot");
56

@@ -144,3 +145,21 @@ if (process.env.DEBUG_INFO) {
144145
require("wast-loader");
145146
process.removeAllListeners("uncaughtException");
146147
process.removeAllListeners("unhandledRejection");
148+
149+
// The runner's `vm.runInThisContext` writes to the worker's own global, which
150+
// outlives this file: drop what the file added so it can't keep it alive.
151+
const outerGlobal = vm.runInThisContext("this");
152+
const outerGlobalKeys = new Set(Object.getOwnPropertyNames(outerGlobal));
153+
154+
// eslint-disable-next-line jest/require-top-level-describe
155+
afterAll(() => {
156+
for (const key of Object.getOwnPropertyNames(outerGlobal)) {
157+
if (outerGlobalKeys.has(key)) continue;
158+
const descriptor =
159+
/** @type {PropertyDescriptor} */
160+
(Object.getOwnPropertyDescriptor(outerGlobal, key));
161+
// a bundle's top-level `var` or function can't be deleted, only cleared
162+
if (descriptor.configurable) delete outerGlobal[key];
163+
else if (descriptor.writable) outerGlobal[key] = undefined;
164+
}
165+
});

‎test/helpers/deprecationTracking.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ util.deprecate = (fn, message, _code) => {
3939
};
4040
};
4141

42+
// util is shared by every test file in a jest worker: restore it, or each
43+
// file's wrapper keeps the previous file's whole environment alive.
44+
afterAll(() => {
45+
util.deprecate = originalDeprecate;
46+
});
47+
4248
/**
4349
* @returns {() => { code: string, message: string, stack: string }[]} result
4450
*/

0 commit comments

Comments
 (0)