Skip to content

Commit

Permalink
Merge pull request #4321 from woss/add-jest-log-heap-usage
Browse files Browse the repository at this point in the history
[heft-jest-plugin] Add logHeapUsage
  • Loading branch information
iclanton authored Sep 14, 2023
2 parents 3ab024c + 01f97b8 commit 4890f4e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/heft-jest-plugin",
"comment": "Add a `--log-heap-usage` flag that includes memory usage analysis in each test run.",
"type": "minor"
}
],
"packageName": "@rushstack/heft-jest-plugin"
}
5 changes: 5 additions & 0 deletions heft-plugins/heft-jest-plugin/heft-plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
"argumentName": "SOURCE_FILE",
"description": "Find and run the tests that cover a source file that was passed in as an argument. This corresponds to the \"--findRelatedTests\" parameter in Jest's documentation. This parameter is not compatible with watch mode."
},
{
"longName": "--log-heap-usage",
"parameterKind": "flag",
"description": "Logs the heap usage after every test. Useful to debug memory leaks. Use together with --expose-gc in node."
},
{
"longName": "--max-workers",
"parameterKind": "string",
Expand Down
8 changes: 7 additions & 1 deletion heft-plugins/heft-jest-plugin/src/HeftJestReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,15 @@ export default class HeftJestReporter implements Reporter {
// (ex. jest-junit) only use perfStats:
// https://github.com/jest-community/jest-junit/blob/12da1a20217a9b6f30858013175319c1256f5b15/utils/buildJsonResults.js#L112
const duration: string = perfStats ? `${((perfStats.end - perfStats.start) / 1000).toFixed(3)}s` : '?';

// calculate memoryUsage to MB reference -> https://jestjs.io/docs/cli#--logheapusage
const memUsage: string = testResult.memoryUsage
? `, ${Math.floor(testResult.memoryUsage / 1000000)}MB heap size`
: '';

const message: string =
` ${this._getTestPath(test.path)} ` +
`(duration: ${duration}, ${numPassingTests} passed, ${numFailingTests} failed)`;
`(duration: ${duration}, ${numPassingTests} passed, ${numFailingTests} failed${memUsage})`;

if (numFailingTests > 0) {
this._terminal.writeLine(Colors.redBackground(Colors.black('FAIL')), message);
Expand Down
4 changes: 4 additions & 0 deletions heft-plugins/heft-jest-plugin/src/JestPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export interface IJestPluginOptions {
testPathPattern?: string;
testTimeout?: number;
updateSnapshots?: boolean;
logHeapUsage?: boolean;
}

export interface IHeftJestConfiguration extends Config.InitialOptions {}
Expand Down Expand Up @@ -187,6 +188,7 @@ export default class JestPlugin implements IHeftTaskPlugin<IJestPluginOptions> {
const silentParameter: CommandLineFlagParameter = parameters.getFlagParameter('--silent');
const updateSnapshotsParameter: CommandLineFlagParameter =
parameters.getFlagParameter('--update-snapshots');
const logHeapUsageParameter: CommandLineFlagParameter = parameters.getFlagParameter('--log-heap-usage');

// Strings
const configParameter: CommandLineStringParameter = parameters.getStringParameter('--config');
Expand All @@ -213,6 +215,7 @@ export default class JestPlugin implements IHeftTaskPlugin<IJestPluginOptions> {
debugHeftReporter: debugHeftReporterParameter.value || pluginOptions?.debugHeftReporter,
detectOpenHandles: detectOpenHandlesParameter.value || pluginOptions?.detectOpenHandles,
disableCodeCoverage: disableCodeCoverageParameter.value || pluginOptions?.disableCodeCoverage,
logHeapUsage: logHeapUsageParameter.value || pluginOptions?.logHeapUsage,
findRelatedTests: findRelatedTestsParameter.values.length
? Array.from(findRelatedTestsParameter.values)
: pluginOptions?.findRelatedTests,
Expand Down Expand Up @@ -574,6 +577,7 @@ export default class JestPlugin implements IHeftTaskPlugin<IJestPluginOptions> {
runInBand: taskSession.parameters.debug,
debug: taskSession.parameters.debug,
detectOpenHandles: options.detectOpenHandles || false,
logHeapUsage: options.logHeapUsage || false,

// Use the temp folder. Cache is unreliable, so we want it cleared on every --clean run
cacheDirectory: taskSession.tempFolderPath,
Expand Down

0 comments on commit 4890f4e

Please sign in to comment.