@@ -9,6 +9,10 @@ import { resolve } from 'pathe'
9
9
import { useTestContext } from './context'
10
10
import { request } from 'undici'
11
11
12
+ function toArray < T > ( value : T | T [ ] ) : T [ ] {
13
+ return Array . isArray ( value ) ? value : [ value ]
14
+ }
15
+
12
16
// @ts -expect-error type cast
13
17
// eslint-disable-next-line
14
18
const kit : typeof _kit = _kit . default || _kit
@@ -17,8 +21,8 @@ export async function startServer(env: Record<string, unknown> = {}) {
17
21
const ctx = useTestContext ( )
18
22
await stopServer ( )
19
23
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 ] } `
22
26
if ( ctx . options . dev ) {
23
27
const nuxiCLI = await kit . resolvePath ( 'nuxi/cli' )
24
28
ctx . serverProcess = exec ( nuxiCLI , [ '_dev' ] , {
@@ -27,15 +31,15 @@ export async function startServer(env: Record<string, unknown> = {}) {
27
31
stdio : 'inherit' ,
28
32
env : {
29
33
...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 ] ) ,
32
36
HOST : host ,
33
37
NODE_ENV : 'development' ,
34
38
...env
35
39
}
36
40
}
37
41
} )
38
- await waitForPort ( port , { retries : 32 , host } ) . catch ( ( ) => { } )
42
+ await waitForPort ( ports [ 0 ] , { retries : 32 , host } ) . catch ( ( ) => { } )
39
43
let lastError
40
44
for ( let i = 0 ; i < 150 ; i ++ ) {
41
45
await new Promise ( resolve => setTimeout ( resolve , 100 ) )
@@ -51,35 +55,36 @@ export async function startServer(env: Record<string, unknown> = {}) {
51
55
ctx . serverProcess . kill ( )
52
56
throw lastError || new Error ( 'Timeout waiting for dev server!' )
53
57
} 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()
56
61
const [ _command , ...commandArgs ] = command . split ( ' ' )
57
62
58
63
ctx . serverProcess = exec ( _command , commandArgs , {
59
64
nodeOptions : {
60
65
env : {
61
66
...process . env ,
62
- PORT : String ( port ) ,
67
+ PORT : String ( ports [ 0 ] ) ,
63
68
HOST : host ,
64
69
...env
65
70
}
66
71
}
67
72
} )
68
73
69
- await waitForPort ( port , { retries : 32 , host, delay : 1000 } )
74
+ await waitForPort ( ports [ 0 ] , { retries : 32 , host, delay : 1000 } )
70
75
} else {
71
76
ctx . serverProcess = exec ( 'node' , [ resolve ( ctx . nuxt ! . options . nitro . output ! . dir ! , 'server/index.mjs' ) ] , {
72
77
nodeOptions : {
73
78
stdio : 'inherit' ,
74
79
env : {
75
80
...process . env ,
76
- PORT : String ( port ) ,
81
+ PORT : String ( ports [ 0 ] ) ,
77
82
HOST : host ,
78
83
...env
79
84
}
80
85
}
81
86
} )
82
- await waitForPort ( port , { retries : 20 , host } )
87
+ await waitForPort ( ports [ 0 ] , { retries : 20 , host } )
83
88
}
84
89
}
85
90
@@ -102,14 +107,21 @@ export function undiciRequest(path: string, options?: Parameters<typeof request>
102
107
return request ( url ( path ) , options )
103
108
}
104
109
105
- export function url ( path : string ) {
110
+ export function url ( path : string , port ?: number ) {
106
111
const ctx = useTestContext ( )
107
112
if ( ! ctx . url ) {
108
113
throw new Error ( 'url is not available (is server option enabled?)' )
109
114
}
115
+
110
116
if ( path . startsWith ( ctx . url ) ) {
111
117
return path
112
118
}
119
+
120
+ // replace port in url
121
+ if ( port != null ) {
122
+ return ctx . url . slice ( 0 , ctx . url . lastIndexOf ( ':' ) ) + `:${ port } /` + path
123
+ }
124
+
113
125
return ctx . url + path
114
126
}
115
127
0 commit comments