Commit b3463fa1 authored by Tobias Koppers's avatar Tobias Koppers

cleanup: deduplicate watcher logic and parametrize .next deletion test

Extract shared restart logic into restartOnDistDirRemoval helper,
use it.each to parametrize app/pages test cases, and remove unused
fixture files that were only needed for the old per-request approach.
Co-Authored-By: 's avatarClaude <noreply@anthropic.com>
parent 4797d2f7
...@@ -224,25 +224,19 @@ export async function initialize(opts: { ...@@ -224,25 +224,19 @@ export async function initialize(opts: {
// Watch the distDir so we can restart the dev server when it's deleted, // Watch the distDir so we can restart the dev server when it's deleted,
// instead of serving broken pages with cryptic ENOENT errors. // instead of serving broken pages with cryptic ENOENT errors.
const distDir = path.join(opts.dir, config.distDir) const distDir = path.join(opts.dir, config.distDir)
try { const distDirLabel =
const distDirWatcher = watch(distDir, () => { (config as NextConfigComplete).distDirRoot || config.distDir
if (!existsSync(distDir)) { const restartOnDistDirRemoval = () => {
distDirWatcher.close() if (!existsSync(distDir)) {
Log.warn(
`The ${(config as NextConfigComplete).distDirRoot || config.distDir} directory was removed while the dev server was running. Restarting...`
)
process.exit(RESTART_EXIT_CODE)
}
})
distDirWatcher.on('error', () => {
// The watcher itself errored (e.g. the directory no longer exists).
// Trigger a restart in that case too.
distDirWatcher.close()
Log.warn( Log.warn(
`The ${(config as NextConfigComplete).distDirRoot || config.distDir} directory was removed while the dev server was running. Restarting...` `The ${distDirLabel} directory was removed while the dev server was running. Restarting...`
) )
process.exit(RESTART_EXIT_CODE) process.exit(RESTART_EXIT_CODE)
}) }
}
try {
const watcher = watch(distDir, restartOnDistDirRemoval)
watcher.on('error', restartOnDistDirRemoval)
} catch { } catch {
// If we can't set up the watcher (e.g. distDir doesn't exist yet), ignore. // If we can't set up the watcher (e.g. distDir doesn't exist yet), ignore.
} }
......
export default function AppOtherPage() {
return <p>app other page</p>
}
...@@ -20,65 +20,37 @@ describe('delete-dot-next-dir', () => { ...@@ -20,65 +20,37 @@ describe('delete-dot-next-dir', () => {
await next.clean() await next.clean()
}) })
it('should recover after .next is deleted (app router)', async () => { it.each([
// 1. Verify app route loads correctly before deletion { router: 'app', path: '/app', expectedText: 'app page' },
await retry(async () => { { router: 'pages', path: '/pages', expectedText: 'pages page' },
const res = await next.fetch('/app') ])(
expect(res.status).toBe(200) 'should recover after .next is deleted ($router router)',
expect(await res.text()).toContain('app page') async ({ path: routePath, expectedText }) => {
}, 30000) // Verify route loads correctly before deletion
await retry(async () => {
// 2. Delete the .next directory while dev server is running const res = await next.fetch(routePath)
await next.deleteFile('.next') expect(res.status).toBe(200)
expect(await res.text()).toContain(expectedText)
// 3. Fetch a page — this triggers error detection and restart }, 30000)
await next.fetch('/app/other').catch(() => {})
// Delete the .next directory while dev server is running
// 4. Wait for the dev server to restart and become ready await next.deleteFile('.next')
await retry(async () => {
const cliOutput = next.cliOutput // Wait for the dev server to detect the deletion and restart
expect(cliOutput).toContain( await retry(async () => {
'The .next directory was removed while the dev server was running. Restarting...' const cliOutput = next.cliOutput
) expect(cliOutput).toContain(
expect(cliOutput).toContain('Ready in') 'The .next directory was removed while the dev server was running. Restarting...'
}, 30000) )
expect(cliOutput).toContain('Ready in')
// 5. After restart, pages should render normally }, 30000)
await retry(async () => {
const res = await next.fetch('/app') // After restart, pages should render normally
expect(res.status).toBe(200) await retry(async () => {
expect(await res.text()).toContain('app page') const res = await next.fetch(routePath)
}, 30000) expect(res.status).toBe(200)
}) expect(await res.text()).toContain(expectedText)
}, 30000)
it('should recover after .next is deleted (pages router)', async () => { }
// 1. Verify pages route loads correctly before deletion )
await retry(async () => {
const res = await next.fetch('/pages')
expect(res.status).toBe(200)
expect(await res.text()).toContain('pages page')
}, 30000)
// 2. Delete the .next directory while dev server is running
await next.deleteFile('.next')
// 3. Fetch a page — this triggers error detection and restart
await next.fetch('/pages/other').catch(() => {})
// 4. Wait for the dev server to restart and become ready
await retry(async () => {
const cliOutput = next.cliOutput
expect(cliOutput).toContain(
'The .next directory was removed while the dev server was running. Restarting...'
)
expect(cliOutput).toContain('Ready in')
}, 30000)
// 5. After restart, pages should render normally
await retry(async () => {
const res = await next.fetch('/pages')
expect(res.status).toBe(200)
expect(await res.text()).toContain('pages page')
}, 30000)
})
}) })
export default function PagesOtherPage() {
return <p>pages other page</p>
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment