@@ -9,6 +9,10 @@ import { resolve } from 'pathe'
99import { useTestContext } from './context'
1010import { request } from 'undici'
1111
12+ function toArray < T > ( value : T | T [ ] ) : T [ ] {
13+ return Array . isArray ( value ) ? value : [ value ]
14+ }
15+
1216// @ts -expect-error type cast
1317// eslint-disable-next-line
1418const kit : typeof _kit = _kit . default || _kit
@@ -17,8 +21,8 @@ export async function startServer(env: Record<string, unknown> = {}) {
1721 const ctx = useTestContext ( )
1822 await stopServer ( )
1923 const host = '127.0.0.1'
20- const port = ctx . options . port || ( await getRandomPort ( host ) )
21- ctx . url = `http://${ host } :${ port } `
24+ const ports = ctx . options . port ? toArray ( ctx . options . port ) : [ await getRandomPort ( host ) ]
25+ ctx . url = `http://${ host } :${ ports [ 0 ] } `
2226 if ( ctx . options . dev ) {
2327 const nuxiCLI = await kit . resolvePath ( 'nuxi/cli' )
2428 ctx . serverProcess = exec ( nuxiCLI , [ '_dev' ] , {
@@ -27,15 +31,15 @@ export async function startServer(env: Record<string, unknown> = {}) {
2731 stdio : 'inherit' ,
2832 env : {
2933 ...process . env ,
30- _PORT : String ( port ) , // Used by internal _dev command
31- PORT : String ( port ) ,
34+ _PORT : String ( ports [ 0 ] ) , // Used by internal _dev command
35+ PORT : String ( ports [ 0 ] ) ,
3236 HOST : host ,
3337 NODE_ENV : 'development' ,
3438 ...env
3539 }
3640 }
3741 } )
38- await waitForPort ( port , { retries : 32 , host } ) . catch ( ( ) => { } )
42+ await waitForPort ( ports [ 0 ] , { retries : 32 , host } ) . catch ( ( ) => { } )
3943 let lastError
4044 for ( let i = 0 ; i < 150 ; i ++ ) {
4145 await new Promise ( resolve => setTimeout ( resolve , 100 ) )
@@ -51,35 +55,36 @@ export async function startServer(env: Record<string, unknown> = {}) {
5155 ctx . serverProcess . kill ( )
5256 throw lastError || new Error ( 'Timeout waiting for dev server!' )
5357 } else if ( ctx . options . prerender ) {
54- const command = `pnpx serve ${ ctx . nuxt ! . options . nitro ! . output ?. publicDir } -l tcp://${ host } :${ port } --no-port-switching`
55- // ; (await import('consola')).consola.restoreConsole()
58+ const listenTo = ports . map ( port => `-l tcp://${ host } :${ port } ` ) . join ( ' ' )
59+ const command = `pnpx serve ${ ctx . nuxt ! . options . nitro ! . output ?. publicDir } ${ listenTo } --no-port-switching`
60+ // ;(await import('consola')).consola.restoreConsole()
5661 const [ _command , ...commandArgs ] = command . split ( ' ' )
5762
5863 ctx . serverProcess = exec ( _command , commandArgs , {
5964 nodeOptions : {
6065 env : {
6166 ...process . env ,
62- PORT : String ( port ) ,
67+ PORT : String ( ports [ 0 ] ) ,
6368 HOST : host ,
6469 ...env
6570 }
6671 }
6772 } )
6873
69- await waitForPort ( port , { retries : 32 , host, delay : 1000 } )
74+ await waitForPort ( ports [ 0 ] , { retries : 32 , host, delay : 1000 } )
7075 } else {
7176 ctx . serverProcess = exec ( 'node' , [ resolve ( ctx . nuxt ! . options . nitro . output ! . dir ! , 'server/index.mjs' ) ] , {
7277 nodeOptions : {
7378 stdio : 'inherit' ,
7479 env : {
7580 ...process . env ,
76- PORT : String ( port ) ,
81+ PORT : String ( ports [ 0 ] ) ,
7782 HOST : host ,
7883 ...env
7984 }
8085 }
8186 } )
82- await waitForPort ( port , { retries : 20 , host } )
87+ await waitForPort ( ports [ 0 ] , { retries : 20 , host } )
8388 }
8489}
8590
@@ -102,14 +107,21 @@ export function undiciRequest(path: string, options?: Parameters<typeof request>
102107 return request ( url ( path ) , options )
103108}
104109
105- export function url ( path : string ) {
110+ export function url ( path : string , port ?: number ) {
106111 const ctx = useTestContext ( )
107112 if ( ! ctx . url ) {
108113 throw new Error ( 'url is not available (is server option enabled?)' )
109114 }
115+
110116 if ( path . startsWith ( ctx . url ) ) {
111117 return path
112118 }
119+
120+ // replace port in url
121+ if ( port != null ) {
122+ return ctx . url . slice ( 0 , ctx . url . lastIndexOf ( ':' ) ) + `:${ port } /` + path
123+ }
124+
113125 return ctx . url + path
114126}
115127
0 commit comments