@@ -15,6 +15,7 @@ vi.mock('@devup-ui/wasm', async (original) => ({
1515 registerTheme : vi . fn ( ) ,
1616 getThemeInterface : vi . fn ( ) ,
1717 getDefaultTheme : vi . fn ( ) ,
18+ getCss : vi . fn ( ( ) => '' ) ,
1819 exportSheet : vi . fn ( ( ) =>
1920 JSON . stringify ( {
2021 css : { } ,
@@ -89,6 +90,15 @@ describe('DevupUINextPlugin', () => {
8990 } )
9091 } )
9192 describe ( 'turbo' , ( ) => {
93+ beforeEach ( ( ) => {
94+ // Mock fetch globally to prevent "http://localhost:undefined" errors
95+ global . fetch = vi . fn ( ( ) => Promise . resolve ( { } as Response ) )
96+ } )
97+
98+ afterEach ( ( ) => {
99+ vi . restoreAllMocks ( )
100+ } )
101+
92102 it ( 'should apply turbo config' , async ( ) => {
93103 vi . stubEnv ( 'TURBOPACK' , '1' )
94104 vi . mocked ( existsSync )
@@ -390,5 +400,56 @@ describe('DevupUINextPlugin', () => {
390400 DEVUP_UI_DEFAULT_THEME : 'light' ,
391401 } )
392402 } )
403+ it ( 'should handle debugPort fetch failure in development mode' , async ( ) => {
404+ vi . stubEnv ( 'TURBOPACK' , '1' )
405+ vi . stubEnv ( 'NODE_ENV' , 'development' )
406+ vi . stubEnv ( 'PORT' , '3000' )
407+ vi . mocked ( existsSync )
408+ . mockReturnValueOnce ( true )
409+ . mockReturnValueOnce ( true )
410+ . mockReturnValueOnce ( true )
411+ . mockReturnValueOnce ( false )
412+ vi . mocked ( writeFileSync ) . mockReturnValue ( )
413+
414+ // Mock process.exit to prevent actual exit
415+ const originalExit = process . exit
416+ const exitSpy = vi . fn ( )
417+ process . exit = exitSpy as any
418+
419+ // Mock process.debugPort
420+ const originalDebugPort = process . debugPort
421+ process . debugPort = 9229
422+
423+ // Mock fetch globally before calling DevupUI
424+ const originalFetch = global . fetch
425+ const fetchMock = vi . fn ( ( url : string | URL ) => {
426+ const urlString = typeof url === 'string' ? url : url . toString ( )
427+ if ( urlString . includes ( '9229' ) ) {
428+ return Promise . reject ( new Error ( 'Connection refused' ) )
429+ }
430+ return Promise . resolve ( { } as Response )
431+ } )
432+ global . fetch = fetchMock as any
433+
434+ // Use fake timers to control setTimeout
435+ vi . useFakeTimers ( )
436+
437+ try {
438+ DevupUI ( { } )
439+
440+ // Wait for the fetch promise to reject (this triggers the catch handler)
441+ // The catch handler sets up a setTimeout, so we need to wait for that
442+ await vi . runAllTimersAsync ( )
443+
444+ // Verify process.exit was called with code 77
445+ expect ( exitSpy ) . toHaveBeenCalledWith ( 77 )
446+ } finally {
447+ // Restore
448+ vi . useRealTimers ( )
449+ global . fetch = originalFetch
450+ process . exit = originalExit
451+ process . debugPort = originalDebugPort
452+ }
453+ } )
393454 } )
394455} )
0 commit comments