Skip to content

Commit

Permalink
#15215 Reduce memory leak in node env by fully uninstalling source-ma…
Browse files Browse the repository at this point in the history
…p-support
  • Loading branch information
eyalroth committed Jul 27, 2024
1 parent 9ff3d3e commit 7423314
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/jest-environment-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {
}

if (this.context) {
// source-map-support keeps memory leftovers in `Error.prepareStackTrace`
runInContext("Error.prepareStackTrace = () => '';", this.context);

// remove any leftover listeners that may hold references to sizable memory
this.context.process.removeAllListeners();
const cluster = runInContext(
Expand Down
8 changes: 6 additions & 2 deletions packages/jest-runner/src/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,12 @@ async function runTestInternal(
sendMessageToJest,
);
} catch (error: any) {
// Access stack before uninstalling sourcemaps
error.stack;
// Access all stacks before uninstalling sourcemaps
let e = error;
while (typeof e === 'object' && 'stack' in e) {
e.stack;
e = e?.cause ?? {};
}

throw error;
} finally {
Expand Down

0 comments on commit 7423314

Please sign in to comment.