@@ -3,38 +3,55 @@ import { enableDebugMode, default as logger } from 'utils/logger';
33
44enableDebugMode ( ) ;
55
6- function addLoggerSpies ( ) {
7- jest . spyOn ( logger , 'error' ) . mockImplementation ( val => console . error ( val as string ) ) ;
8- jest . spyOn ( logger , 'log' ) . mockImplementation ( val => console . log ( val as string ) ) ;
9- jest . spyOn ( logger , 'info' ) . mockImplementation ( val => console . info ( val as string ) ) ;
10- jest . spyOn ( logger , 'warn' ) . mockImplementation ( val => console . warn ( val as string ) ) ;
11- jest . spyOn ( logger , 'table' ) . mockImplementation ( val => console . table ( val as string ) ) ;
12- jest . spyOn ( logger , 'success' ) . mockImplementation ( val => console . info ( val as string ) ) ;
13- jest . spyOn ( logger , 'debug' ) . mockImplementation ( val => {
14- if ( val instanceof Error ) {
15- return console . error ( val ) ;
16- }
6+ // Store logger spies so they can be properly cleaned up
7+ let loggerSpies : jest . SpyInstance [ ] = [ ] ;
178
18- console . debug ( val as string ) ;
9+ function addLoggerSpies ( ) {
10+ // Restore and clear any existing spies first
11+ loggerSpies . forEach ( spy => {
12+ spy . mockRestore ( ) ;
1913 } ) ;
14+ loggerSpies = [ ] ;
15+
16+ // Create new spies that write to process.stdout/stderr so they can be captured
17+ loggerSpies . push (
18+ jest . spyOn ( logger , 'error' ) . mockImplementation ( val => process . stderr . write ( val as string + '\n' ) ) ,
19+ jest . spyOn ( logger , 'log' ) . mockImplementation ( val => process . stdout . write ( val as string + '\n' ) ) ,
20+ jest . spyOn ( logger , 'info' ) . mockImplementation ( val => process . stdout . write ( val as string + '\n' ) ) ,
21+ jest . spyOn ( logger , 'warn' ) . mockImplementation ( val => process . stderr . write ( val as string + '\n' ) ) ,
22+ jest . spyOn ( logger , 'table' ) . mockImplementation ( val => process . stdout . write ( JSON . stringify ( val ) as string + '\n' ) ) ,
23+ jest . spyOn ( logger , 'success' ) . mockImplementation ( val => process . stdout . write ( val as string + '\n' ) ) ,
24+ jest . spyOn ( logger , 'debug' ) . mockImplementation ( val => {
25+ if ( val instanceof Error ) {
26+ return process . stderr . write ( val . toString ( ) + '\n' ) ;
27+ }
28+
29+ process . stderr . write ( val as string + '\n' ) ;
30+ } ) ,
31+ ) ;
2032}
2133
2234global . beforeEach ( ( ) => {
2335 addLoggerSpies ( ) ;
2436 getConfigDataByKeySpy . mockReturnValue ( 'mocked-access-token' ) ;
2537 // @ts -ignore
26- processExistSpy . mockImplementation ( code => {
38+ processExistSpy . mockImplementation ( ( code ) => {
2739 if ( code !== 0 ) {
2840 throw new Error ( `process.exit(${ code } )` ) ;
2941 }
42+ // Return undefined to prevent actual process exit
43+ return undefined as never ;
3044 } ) ;
3145} ) ;
3246
3347global . afterEach ( ( ) => {
34- getConfigDataByKeySpy . mockReset ( ) ;
35- processExistSpy . mockReset ( ) ;
36- stderrWriteSpy . mockReset ( ) ;
37- stdoutWriteSpy . mockReset ( ) ;
48+ // Clear all spies
49+ loggerSpies . forEach ( spy => spy . mockClear ( ) ) ;
50+
51+ getConfigDataByKeySpy . mockClear ( ) ;
52+ processExistSpy . mockClear ( ) ;
53+ stderrWriteSpy . mockClear ( ) ;
54+ stdoutWriteSpy . mockClear ( ) ;
3855} ) ;
3956
4057process . on ( 'unhandledRejection' , err => {
0 commit comments