Skip to content

Commit

Permalink
Memory leak fix: release console output reference after printed to st…
Browse files Browse the repository at this point in the history
…dout. (#8233)

* Memory leak fix: release console output reference after printed to stdout.

* Resolve type errors.

* Return undefined if no console output.

* Update CHANGELOG.md

* Move memory release out of reporter.
  • Loading branch information
scotthovestadt authored Mar 29, 2019
1 parent 384a0d9 commit 84466b7
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
- `[jest-core]` Improve performance of SearchSource.findMatchingTests by 15% ([#8184](https://github.com/facebook/jest/pull/8184))
- `[jest-resolve]` Optimize internal cache lookup performance ([#8183](https://github.com/facebook/jest/pull/8183))
- `[jest-core]` Dramatically improve watch mode performance ([#8201](https://github.com/facebook/jest/pull/8201))
- `[jest-console]` Fix memory leak by releasing console output reference when printed to stdout ([#8233](https://github.com/facebook/jest/pull/8233))
- `[jest-runtime]` Use `Map` instead of `Object` for module registry ([#8232](https://github.com/facebook/jest/pull/8232))

## 24.5.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export const runAndTransformResultsToJestFormat = async ({

dispatch({name: 'teardown'});
return {
console: null,
console: undefined,
displayName: config.displayName,
failureMessage,
leaks: false, // That's legacy code, just adding it so Flow is happy.
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-console/src/BufferedConsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export default class BufferedConsole extends Console {
this._log('warn', format(firstArg, ...rest));
}

getBuffer(): ConsoleBuffer {
return this._buffer;
getBuffer() {
return this._buffer.length ? this._buffer : undefined;
}
}
2 changes: 1 addition & 1 deletion packages/jest-console/src/CustomConsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,6 @@ export default class CustomConsole extends Console {
}

getBuffer() {
return null;
return undefined;
}
}
13 changes: 8 additions & 5 deletions packages/jest-console/src/__tests__/bufferedConsole.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import BufferedConsole from '../BufferedConsole';

describe('CustomConsole', () => {
let _console: BufferedConsole;
const stdout = () =>
_console
.getBuffer()
.map(log => log.message)
.join('\n');
const stdout = () => {
const buffer = _console.getBuffer();
if (!buffer) {
return '';
}

return buffer.map(log => log.message).join('\n');
};

beforeEach(() => {
_console = new BufferedConsole(() => null);
Expand Down
3 changes: 3 additions & 0 deletions packages/jest-core/src/ReporterDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export default class ReporterDispatcher {
reporter.onTestResult &&
(await reporter.onTestResult(test, testResult, results));
}

// Release memory if unused later.
testResult.console = undefined;
}

async onTestStart(test: Test) {
Expand Down
5 changes: 2 additions & 3 deletions packages/jest-reporters/src/default_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,15 @@ export default class DefaultReporter extends BaseReporter {
result: TestResult,
) {
this.log(getResultHeader(result, this._globalConfig, config));
const consoleBuffer = result.console;
if (consoleBuffer && consoleBuffer.length) {
if (result.console) {
this.log(
' ' +
TITLE_BULLET +
'Console\n\n' +
getConsoleOutput(
config.cwd,
!!this._globalConfig.verbose,
consoleBuffer,
result.console,
),
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-test-result/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const buildFailureTestResult = (
testPath: Config.Path,
err: SerializableError,
): TestResult => ({
console: null,
console: undefined,
displayName: '',
failureMessage: null,
leaks: false,
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-test-result/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export type Suite = {
};

export type TestResult = {
console?: ConsoleBuffer | null;
console?: ConsoleBuffer;
coverage?: CoverageMapData;
displayName?: Config.DisplayName;
failureMessage?: string | null;
Expand Down

0 comments on commit 84466b7

Please sign in to comment.