|
| 1 | +/** |
| 2 | + * Global Jest Setup File |
| 3 | + * |
| 4 | + * 1. Captures console logs for test reports |
| 5 | + * 2. Suppresses expected SDK validation errors to reduce console noise during tests. |
| 6 | + */ |
| 7 | +import * as fs from 'fs'; |
| 8 | +import * as path from 'path'; |
| 9 | + |
| 10 | +// Store captured console logs |
| 11 | +interface ConsoleLog { |
| 12 | + type: 'log' | 'warn' | 'error' | 'info' | 'debug'; |
| 13 | + message: string; |
| 14 | + timestamp: string; |
| 15 | + testFile?: string; |
| 16 | +} |
| 17 | + |
| 18 | +declare global { |
| 19 | + var __CONSOLE_LOGS__: ConsoleLog[]; |
| 20 | + var __CURRENT_TEST_FILE__: string; |
| 21 | +} |
| 22 | + |
| 23 | +// Initialize global console log storage |
| 24 | +global.__CONSOLE_LOGS__ = []; |
| 25 | +global.__CURRENT_TEST_FILE__ = ''; |
| 26 | + |
| 27 | +// Store original console methods |
| 28 | +const originalConsole = { |
| 29 | + log: console.log, |
| 30 | + warn: console.warn, |
| 31 | + error: console.error, |
| 32 | + info: console.info, |
| 33 | + debug: console.debug |
| 34 | +}; |
| 35 | + |
| 36 | +// List of expected SDK validation errors to suppress |
| 37 | +const expectedErrors = [ |
| 38 | + 'Invalid key:', // From query.search() validation |
| 39 | + 'Invalid value (expected string or number):', // From query.equalTo() validation |
| 40 | + 'Argument should be a String or an Array.', // From entry/entries.includeReference() validation |
| 41 | + 'Invalid fieldUid:', // From asset query validation |
| 42 | +]; |
| 43 | + |
| 44 | +// Helper to capture and optionally forward console output |
| 45 | +function captureConsole(type: 'log' | 'warn' | 'error' | 'info' | 'debug') { |
| 46 | + return (...args: any[]) => { |
| 47 | + const message = args.map(arg => |
| 48 | + typeof arg === 'object' ? JSON.stringify(arg, null, 2) : String(arg) |
| 49 | + ).join(' '); |
| 50 | + |
| 51 | + // Store the log |
| 52 | + global.__CONSOLE_LOGS__.push({ |
| 53 | + type, |
| 54 | + message, |
| 55 | + timestamp: new Date().toISOString(), |
| 56 | + testFile: global.__CURRENT_TEST_FILE__ |
| 57 | + }); |
| 58 | + |
| 59 | + // For errors, check if it's expected (suppress if so) |
| 60 | + if (type === 'error') { |
| 61 | + const isExpectedError = expectedErrors.some(pattern => message.includes(pattern)); |
| 62 | + if (!isExpectedError) { |
| 63 | + originalConsole[type].apply(console, args); |
| 64 | + } |
| 65 | + } else { |
| 66 | + // Forward other logs normally |
| 67 | + originalConsole[type].apply(console, args); |
| 68 | + } |
| 69 | + }; |
| 70 | +} |
| 71 | + |
| 72 | +// Override console methods to capture logs |
| 73 | +console.log = captureConsole('log'); |
| 74 | +console.warn = captureConsole('warn'); |
| 75 | +console.error = captureConsole('error'); |
| 76 | +console.info = captureConsole('info'); |
| 77 | +console.debug = captureConsole('debug'); |
| 78 | + |
| 79 | +// After all tests complete, write logs to file |
| 80 | +afterAll(() => { |
| 81 | + const logsPath = path.resolve(__dirname, 'test-results', 'console-logs.json'); |
| 82 | + const logsDir = path.dirname(logsPath); |
| 83 | + |
| 84 | + if (!fs.existsSync(logsDir)) { |
| 85 | + fs.mkdirSync(logsDir, { recursive: true }); |
| 86 | + } |
| 87 | + |
| 88 | + // Append to existing logs (in case of multiple test files) |
| 89 | + let existingLogs: ConsoleLog[] = []; |
| 90 | + if (fs.existsSync(logsPath)) { |
| 91 | + try { |
| 92 | + existingLogs = JSON.parse(fs.readFileSync(logsPath, 'utf8')); |
| 93 | + } catch { |
| 94 | + existingLogs = []; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + const allLogs = [...existingLogs, ...global.__CONSOLE_LOGS__]; |
| 99 | + fs.writeFileSync(logsPath, JSON.stringify(allLogs, null, 2)); |
| 100 | + |
| 101 | + // Clear for next file |
| 102 | + global.__CONSOLE_LOGS__ = []; |
| 103 | +}); |
| 104 | + |
0 commit comments