Skip to content

Commit eaa5ab7

Browse files
authored
Merge pull request #496 from dev-five-git/fix-next-turbo-startup-issue
Fix turbo boot issue
2 parents 91ae2cd + 480fd0c commit eaa5ab7

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"changes": { "packages/next-plugin/package.json": "Patch" },
3+
"note": "Add forced boot code",
4+
"date": "2025-11-19T08:36:56.984126800Z"
5+
}

packages/next-plugin/src/__tests__/plugin.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
})

packages/next-plugin/src/plugin.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,13 @@ export function DevupUI(
8383
)
8484

8585
if (process.env.NODE_ENV !== 'production') {
86-
// dev
86+
// check if debugger is attached
87+
fetch('http://localhost:' + process.env.PORT)
88+
fetch('http://localhost:' + process.debugPort).catch(() => {
89+
setTimeout(() => {
90+
process.exit(77)
91+
}, 500)
92+
})
8793
process.env.TURBOPACK_DEBUG_JS = '*'
8894
process.env.NODE_OPTIONS ??= ''
8995
process.env.NODE_OPTIONS += ' --inspect-brk'

0 commit comments

Comments
 (0)