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

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

Merged
merged 6 commits into from Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
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))

## 24.5.0

Expand Down
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
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
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
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
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
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
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
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