Skip to content

Commit 593afab

Browse files
OttoAllmendingerllm-git
andcommitted
feat(utxo-bin): add utility for capturing console output in tests
Add helper function to capture stdout/stderr during test execution to make assertions on console output. Includes disabling colors for reliable tests. Issue: BTC-2170 Co-authored-by: llm-git <[email protected]>
1 parent 184859d commit 593afab

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import * as util from 'node:util';
2+
3+
export async function captureConsole<T>(
4+
func: () => Promise<T>
5+
): Promise<{ stdout: string; stderr: string; result: T }> {
6+
process.env.NO_COLOR = '1'; // Disable colors in console output for easier testing
7+
const oldConsoleLog = console.log;
8+
const oldConsoleError = console.error;
9+
10+
const stdoutData: string[] = [];
11+
const stderrData: string[] = [];
12+
13+
console.log = (...args: any[]) => {
14+
stdoutData.push(util.format(...args));
15+
};
16+
17+
console.error = (...args: any[]) => {
18+
stderrData.push(util.format(...args));
19+
};
20+
21+
let result: T;
22+
try {
23+
result = await func();
24+
} finally {
25+
console.log = oldConsoleLog;
26+
console.error = oldConsoleError;
27+
}
28+
29+
const join = (data: string[]) => (data.length > 0 ? data.join('\n') + '\n' : '');
30+
31+
return {
32+
stdout: join(stdoutData),
33+
stderr: join(stderrData),
34+
result,
35+
};
36+
}

0 commit comments

Comments
 (0)