@@ -71,7 +71,10 @@ const startServer = async ({
7171 skipWaitPort = false ,
7272 targetPort,
7373} : DevServerOptions ) : Promise < DevServer | { timeout : boolean ; output : string } > => {
74- const port = await getPort ( )
74+ // Use worker-specific port ranges to avoid conflicts in parallel execution
75+ const workerId = process . env . VITEST_WORKER_ID || '1'
76+ const workerOffset = ( parseInt ( workerId ) - 1 ) * 100
77+ const port = await getPort ( { port : getPort . makeRange ( 3000 + workerOffset , 3000 + workerOffset + 99 ) } )
7578 const host = 'localhost'
7679 const url = `http://${ host } :${ port } `
7780
@@ -89,7 +92,8 @@ const startServer = async ({
8992 if ( targetPort ) {
9093 baseArgs . push ( '--target-port' , targetPort . toString ( ) )
9194 } else {
92- const staticPort = await getPort ( )
95+ // Use same worker-specific range for static port
96+ const staticPort = await getPort ( { port : getPort . makeRange ( 4000 + workerOffset , 4000 + workerOffset + 99 ) } )
9397 baseArgs . push ( '--staticServerPort' , staticPort . toString ( ) )
9498 }
9599
@@ -158,7 +162,18 @@ const startServer = async ({
158162 } ,
159163 close : async ( ) => {
160164 selfKilled = true
161- await killProcess ( ps )
165+ // Enhanced cleanup for Windows to handle Next.js child processes
166+ if ( process . platform === 'win32' && ps . pid ) {
167+ try {
168+ // Kill process tree on Windows to ensure all child processes are terminated
169+ await execa ( 'taskkill' , [ '/pid' , ps . pid . toString ( ) , '/T' , '/F' ] )
170+ } catch ( error ) {
171+ // Fallback to standard kill if taskkill fails
172+ await killProcess ( ps )
173+ }
174+ } else {
175+ await killProcess ( ps )
176+ }
162177 } ,
163178 promptHistory,
164179 } )
0 commit comments