|
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); |
16 | 77 | }; |
17 | 78 |
|
18 | 79 | const originalBeforeEach = global.beforeEach; |
|
0 commit comments