diff --git a/test/dev.ts b/test/dev.ts index 21432f3a51..583ab613a9 100644 --- a/test/dev.ts +++ b/test/dev.ts @@ -9,7 +9,7 @@ import { loadEnv } from 'payload/node' import { getNextRootDir } from './helpers/getNextRootDir.js' import { runInit } from './runInit.js' -import { safelyRunScriptFunction } from './safelyRunScript.js' +import { child, safelyRunScriptFunction } from './safelyRunScript.js' import { createTestHooks } from './testHooks.js' const prod = process.argv.includes('--prod') @@ -59,6 +59,15 @@ await nextDev({ port }, 'default', rootDir) void fetch(`http://localhost:${port}${adminRoute}`) // This ensures that the next-server process is killed when this process is killed and doesn't linger around. -process.on('SIGINT', function () { +process.on('SIGINT', () => { + if (child) { + child.kill('SIGINT') + } process.exit(0) }) +process.on('SIGTERM', () => { + if (child) { + child.kill('SIGINT') + } + process.exit(0) // Exit the parent process +}) diff --git a/test/safelyRunScript.ts b/test/safelyRunScript.ts index 94c579fabc..706bc0930c 100644 --- a/test/safelyRunScript.ts +++ b/test/safelyRunScript.ts @@ -1,6 +1,8 @@ import { spawn } from 'child_process' import path from 'path' +export let child + /** * Sometimes, running certain functions in certain scripts from the command line will cause the script to be terminated * with a "Detected unsettled top-level await" error. This often happens if that function imports the payload config. @@ -29,7 +31,7 @@ export async function safelyRunScriptFunction( }) } -function restartProcess(reason: string): never { +function restartProcess(reason: string) { console.warn(`Restarting process: ${reason}`) // Get the path to the current script @@ -37,14 +39,18 @@ function restartProcess(reason: string): never { const absoluteScriptPath = path.resolve(scriptPath) // Spawn a new process - const child = spawn('tsx', [absoluteScriptPath, ...process.argv.slice(2)], { + child = spawn('tsx', [absoluteScriptPath, ...process.argv.slice(2)], { stdio: 'inherit', - detached: true, + // detached: true, }) - // Unref the child process so the parent can exit - child.unref() - - // Exit the current process - process.exit(0) + // Setup signal handlers to ensure child process exits correctly + process.on('SIGINT', () => { + child.kill('SIGINT') // Forward SIGINT to child process + process.exit(0) // Exit the parent process + }) + process.on('SIGTERM', () => { + child.kill('SIGTERM') // Forward SIGTERM to child process + process.exit(0) // Exit the parent process + }) }