Skip to content

Commit

Permalink
jestjs#15215 Reduce memory leak in node env by clearing event listene…
Browse files Browse the repository at this point in the history
…rs in teardown
  • Loading branch information
eyalroth committed Jul 27, 2024
1 parent a124e2e commit 9ff3d3e
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion packages/jest-environment-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,23 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {
if (this.fakeTimersModern) {
this.fakeTimersModern.dispose();
}
this.context = null;

if (this.context) {
// remove any leftover listeners that may hold references to sizable memory
this.context.process.removeAllListeners();
const cluster = runInContext(
"require('node:cluster')",
Object.assign(this.context, {
require:
// get native require instead of webpack's
// @ts-expect-error https://webpack.js.org/api/module-variables/#__non_webpack_require__-webpack-specific
__non_webpack_require__,
}),
);
cluster.removeAllListeners();

this.context = null;
}
this.fakeTimers = null;
this.fakeTimersModern = null;
this._globalProxy.clear();
Expand Down

0 comments on commit 9ff3d3e

Please sign in to comment.