Skip to content

Commit 7423314

Browse files
committed
#15215 Reduce memory leak in node env by fully uninstalling source-map-support
1 parent 9ff3d3e commit 7423314

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

packages/jest-environment-node/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {
216216
}
217217

218218
if (this.context) {
219+
// source-map-support keeps memory leftovers in `Error.prepareStackTrace`
220+
runInContext("Error.prepareStackTrace = () => '';", this.context);
221+
219222
// remove any leftover listeners that may hold references to sizable memory
220223
this.context.process.removeAllListeners();
221224
const cluster = runInContext(

packages/jest-runner/src/runTest.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,12 @@ async function runTestInternal(
312312
sendMessageToJest,
313313
);
314314
} catch (error: any) {
315-
// Access stack before uninstalling sourcemaps
316-
error.stack;
315+
// Access all stacks before uninstalling sourcemaps
316+
let e = error;
317+
while (typeof e === 'object' && 'stack' in e) {
318+
e.stack;
319+
e = e?.cause ?? {};
320+
}
317321

318322
throw error;
319323
} finally {

0 commit comments

Comments
 (0)