Skip to content

Commit 0514573

Browse files
authored
Silencing the tests that are passing (#5)
When rendering code the logs of successful tests are not bringing any value while they create substantial amount of tokens thus polluting the context. We therefore monkey patch jest setup to silence the tests that are passing. Work item: https://app.devrev.ai/devrev/works/ISS-188589
1 parent 44e5d29 commit 0514573

File tree

3 files changed

+78
-17
lines changed

3 files changed

+78
-17
lines changed

jest.setup.js

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,79 @@
1-
// Propagate the logs, but only show warnings and errors
2-
// Problems of this propagation approach: Node.js prints log buffer in bulk, so console logs and the associated tests that fail don't get placed together to one another.
3-
// Let's try to keep it like that for now and see the results, but let's keep in mind that this still has room for improvement.
4-
// Override console to only show warnings and errors
5-
console.info('Jest custom setup installed.');
6-
7-
const originalConsole = global.console;
8-
global.console = {
9-
...originalConsole,
10-
log: () => {}, // Suppress console.log
11-
info: () => {}, // Suppress console.info
12-
debug: () => {}, // Suppress console.debug
13-
// Keep warn and error
14-
warn: originalConsole.warn,
15-
error: originalConsole.error,
1+
let logBuffer = [];
2+
3+
const methods = ['log', 'info', 'warn', 'error'];
4+
5+
const formatArg = (arg) => {
6+
if (typeof arg === 'string') return arg;
7+
if (typeof arg === 'number' || typeof arg === 'boolean' || arg === null) return String(arg);
8+
try {
9+
return JSON.stringify(arg, null, 2);
10+
} catch {
11+
return '[Unserializable Object]';
12+
}
13+
};
14+
15+
const originalConsole = {};
16+
17+
// Override console methods to buffer logs
18+
methods.forEach((method) => {
19+
originalConsole[method] = console[method];
20+
console[method] = (...args) => {
21+
logBuffer.push({ method, args });
22+
};
23+
});
24+
25+
// Monkey-patch test to track failure
26+
const originalTest = global.test;
27+
28+
global.test = (name, fn, timeout) => {
29+
originalTest(name, async () => {
30+
let failed = false;
31+
logBuffer = [];
32+
33+
try {
34+
await fn();
35+
} catch (err) {
36+
failed = true;
37+
throw err;
38+
} finally {
39+
if (failed && logBuffer.length > 0) {
40+
const { testPath, currentTestName } = expect.getState();
41+
42+
const suite = currentTestName?.replace(name, '').trim() || '(unknown suite)';
43+
44+
const start_banner = [
45+
'─'.repeat(12),
46+
`Test "${suite}" > "${name}" failed — console output follows:`,
47+
'─'.repeat(12),
48+
'',
49+
].join('\n');
50+
51+
process.stdout.write(start_banner + '\n');
52+
53+
logBuffer.forEach(({ method, args }) => {
54+
const prefix = {
55+
log: 'console.log ',
56+
info: 'console.info',
57+
warn: 'console.warn',
58+
error: 'console.error',
59+
}[method] || 'console.log';
60+
61+
const msg = args.map(formatArg).join(' ');
62+
63+
process.stdout.write(`${prefix}: ${msg}\n\n`);
64+
});
65+
66+
const end_banner = [
67+
'─'.repeat(12),
68+
`End of console output.`,
69+
'─'.repeat(12),
70+
'',
71+
].join('\n');
72+
73+
process.stdout.write(end_banner + '\n');
74+
}
75+
}
76+
}, timeout);
1677
};
1778

1879
const originalBeforeEach = global.beforeEach;

run_devrev_snapin_conformance_tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ if [ "${VERBOSE:-}" -eq 1 ] 2>/dev/null; then
276276
printf "Running conformance tests...\n"
277277
fi
278278

279-
npm test -- --runInBand --silent --setupFilesAfterEnv="$SCRIPT_DIR/jest.setup.js" --detectOpenHandles 2>&1 | sed -E "$ANSI_ESCAPE_PATTERN"
279+
npm test -- --runInBand --setupFilesAfterEnv="$SCRIPT_DIR/jest.setup.js" --detectOpenHandles 2>&1 | sed -E "$ANSI_ESCAPE_PATTERN"
280280
conformance_tests_result=$?
281281

282282
if [ $conformance_tests_result -ne 0 ]; then

run_unittests_jest.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ rm "$build_output"
7575

7676
printf "### Step 2: Running unittests in $FOLDER_NAME...\n"
7777

78-
npm test -- --runInBand --silent --setupFilesAfterEnv="$SCRIPT_DIR/jest.setup.js" --detectOpenHandles 2>&1 | sed -E "$ANSI_ESCAPE_PATTERN"
78+
npm test -- --runInBand --setupFilesAfterEnv="$SCRIPT_DIR/jest.setup.js" --detectOpenHandles 2>&1 | sed -E "$ANSI_ESCAPE_PATTERN"
7979
TEST_EXIT_CODE=$?
8080

8181
# Check if tests failed

0 commit comments

Comments
 (0)