Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fully cleanup source-map-support #15233

Merged
merged 1 commit into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@

- `[*]` [**BREAKING**] Bundle all of Jest's modules into `index.js` ([#12348](https://github.com/jestjs/jest/pull/12348), [#14550](https://github.com/jestjs/jest/pull/14550) & [#14661](https://github.com/jestjs/jest/pull/14661))
- `[jest-haste-map]` Only spawn one process to check for `watchman` installation ([#14826](https://github.com/jestjs/jest/pull/14826))
- `[jest-runner]` Better cleanup `source-map-support` after test to resolve (minor) memory leak ([#15233](https://github.com/jestjs/jest/pull/15233))

### Chore & Maintenance

Expand Down
19 changes: 15 additions & 4 deletions packages/jest-runner/src/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*
*/

import {runInContext} from 'node:vm';
import chalk = require('chalk');
import * as fs from 'graceful-fs';
import sourcemapSupport = require('source-map-support');
Expand Down Expand Up @@ -208,6 +209,14 @@ async function runTestInternal(
const tearDownEnv = async () => {
if (!isTornDown) {
runtime.teardown();

// source-map-support keeps memory leftovers in `Error.prepareStackTrace`
runInContext(
"Error.prepareStackTrace = () => '';",
environment.getVmContext()!,
);
sourcemapSupport.resetRetrieveHandlers();

await environment.teardown();
isTornDown = true;
}
Expand Down Expand Up @@ -312,8 +321,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' && e !== null && 'stack' in e) {
e.stack;
e = e?.cause;
}

throw error;
} finally {
Expand Down Expand Up @@ -377,8 +390,6 @@ async function runTestInternal(
});
} finally {
await tearDownEnv();

sourcemapSupport.resetRetrieveHandlers();
}
}

Expand Down
Loading