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 4 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 @@ -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
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
6 changes: 3 additions & 3 deletions packages/jest-reporters/src/default_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,18 @@ 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,
),
);
result.console = undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the reporter should be responsible for mutating the testresult object. The memory consumption would be reintroduced with custom reporters. Can we move this to the runner or jest-core instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great feedback, moved to jest-core. To be honest, I just blindly followed the pattern of how coverage was previously released without thinking about it too carefully. Thanks for your review.

}
}

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