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

chore: fix memory overflow for buffered console #32986

Merged
merged 5 commits into from
Jan 17, 2025
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
18 changes: 15 additions & 3 deletions packages/aws-cdk-lib/testhelpers/jest-bufferedconsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ export default class TestEnvironment extends NodeEnvironment implements JestEnvi
// doesn't work properly.
(this as JestEnvironment<unknown>).handleTestEvent = (async (event, _state) => {
if (event.name === 'test_done' && event.test.errors.length > 0 && this.log.length > 0) {
this.stopCapture();

this.originalConsole.log(`[Console output] ${fullTestName(event.test)}\n`);
for (const item of this.log) {
this.originalConsole[item.type](' ' + item.message);
}
this.originalConsole.log('\n');

this.startCapture();
}

if (event.name === 'test_done') {
Expand All @@ -43,6 +47,15 @@ export default class TestEnvironment extends NodeEnvironment implements JestEnvi
await super.setup();

this.log = [];
this.startCapture();
}

async teardown() {
this.stopCapture();
await super.teardown();
}

private startCapture() {
this.originalConsole = console;
this.originalStdoutWrite = process.stdout.write;
this.originalStderrWrite = process.stderr.write;
Expand Down Expand Up @@ -75,11 +88,10 @@ export default class TestEnvironment extends NodeEnvironment implements JestEnvi
} as any;
}

async teardown() {
private stopCapture() {
this.global.console = this.originalConsole;
process.stdout.write = this.originalStdoutWrite;
process.stderr.write = this.originalStderrWrite;
await super.teardown();
}
}

Expand All @@ -92,7 +104,7 @@ type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>
function fullTestName(test: TestDescription) {
let ret = test.name;
while (test.parent != null && test.parent.name !== 'ROOT_DESCRIBE_BLOCK') {
ret = test.parent.name + ' › ' + fullTestName;
ret = test.parent.name + ' › ' + ret;
test = test.parent;
}
return ret;
Expand Down
1 change: 1 addition & 0 deletions packages/aws-cdk/lib/version.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* istanbul ignore file */
import * as path from 'path';
import * as chalk from 'chalk';
import * as fs from 'fs-extra';
Expand Down
18 changes: 15 additions & 3 deletions packages/aws-cdk/test/jest-bufferedconsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ export default class TestEnvironment extends NodeEnvironment implements JestEnvi
// doesn't work properly.
(this as JestEnvironment<unknown>).handleTestEvent = (async (event, _state) => {
if (event.name === 'test_done' && event.test.errors.length > 0 && this.log.length > 0) {
this.stopCapture();

this.originalConsole.log(`[Console output] ${fullTestName(event.test)}\n`);
for (const item of this.log) {
this.originalConsole[item.type](' ' + item.message);
}
this.originalConsole.log('\n');

this.startCapture();
}

if (event.name === 'test_done') {
Expand All @@ -43,6 +47,15 @@ export default class TestEnvironment extends NodeEnvironment implements JestEnvi
await super.setup();

this.log = [];
this.startCapture();
}

async teardown() {
this.stopCapture();
await super.teardown();
}

private startCapture() {
this.originalConsole = console;
this.originalStdoutWrite = process.stdout.write;
this.originalStderrWrite = process.stderr.write;
Expand Down Expand Up @@ -75,11 +88,10 @@ export default class TestEnvironment extends NodeEnvironment implements JestEnvi
} as any;
}

async teardown() {
private stopCapture() {
this.global.console = this.originalConsole;
process.stdout.write = this.originalStdoutWrite;
process.stderr.write = this.originalStderrWrite;
await super.teardown();
}
}

Expand All @@ -92,7 +104,7 @@ type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>
function fullTestName(test: TestDescription) {
let ret = test.name;
while (test.parent != null && test.parent.name !== 'ROOT_DESCRIBE_BLOCK') {
ret = test.parent.name + ' › ' + fullTestName;
ret = test.parent.name + ' › ' + ret;
test = test.parent;
}
return ret;
Expand Down
Loading