11/**
22 * Global Jest Setup File
33 *
4- * Suppresses expected SDK validation errors to reduce console noise during tests.
5- * These errors are intentional - tests verify that the SDK handles invalid inputs gracefully .
4+ * 1. Captures console logs for test reports
5+ * 2. Suppresses expected SDK validation errors to reduce console noise during tests .
66 */
7+ import * as fs from 'fs' ;
8+ import * as path from 'path' ;
79
8- // Store the original console.error for genuine errors
9- const originalConsoleError = console . error ;
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+ } ;
1035
1136// List of expected SDK validation errors to suppress
1237const expectedErrors = [
@@ -16,17 +41,64 @@ const expectedErrors = [
1641 'Invalid fieldUid:' , // From asset query validation
1742] ;
1843
19- // Override console.error globally to filter expected validation errors
20- console . error = ( ...args : any [ ] ) => {
21- const message = args [ 0 ] ?. toString ( ) || '' ;
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 ) ;
2283
23- // Check if this is an expected SDK validation error
24- const isExpectedError = expectedErrors . some ( pattern => message . includes ( pattern ) ) ;
84+ if ( ! fs . existsSync ( logsDir ) ) {
85+ fs . mkdirSync ( logsDir , { recursive : true } ) ;
86+ }
2587
26- // If not expected, show it (for genuine errors)
27- if ( ! isExpectedError ) {
28- originalConsoleError . apply ( console , args ) ;
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+ }
2996 }
30- // Otherwise, silently suppress it
31- } ;
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+ } ) ;
32104
0 commit comments