@@ -35,7 +35,7 @@ type SupportStatus = boolean | {
3535 websockets : boolean
3636}
3737
38- function createIt ( runtimeName : typeof runtimes [ number ] , _socketsEnabled : boolean ) {
38+ function createIt ( runtimeName : typeof runtimes [ number ] ) {
3939 function it ( description : string , fn : ( ) => Promise < void > ) : void
4040 function it ( description : string , options : TestOptions , fn : ( ) => Promise < void > ) : void
4141 function it ( description : string , _options : TestOptions | ( ( ) => Promise < void > ) , _fn ?: ( ) => Promise < void > ) : void {
@@ -89,176 +89,168 @@ function createIt(runtimeName: typeof runtimes[number], _socketsEnabled: boolean
8989 return it
9090}
9191
92- const socketConfigs = [
93- { enabled : true , label : 'with sockets' } ,
94- { enabled : false , label : 'without sockets' } ,
95- ] as const
96-
9792describe . sequential . each ( runtimes ) ( 'dev server (%s)' , ( runtimeName ) => {
98- describe . sequential . each ( socketConfigs ) ( '$label' , ( { enabled : socketsEnabled } ) => {
99- let server : DevServerInstance
93+ let server : DevServerInstance
10094
101- if ( ! isCI && ! runtime [ runtimeName ] ) {
102- console . warn ( `Not testing locally with ${ runtimeName } as it is not installed.` )
103- _it . skip ( `should pass with ${ runtimeName } ` )
104- return
105- }
95+ if ( ! isCI && ! runtime [ runtimeName ] ) {
96+ console . warn ( `Not testing locally with ${ runtimeName } as it is not installed.` )
97+ _it . skip ( `should pass with ${ runtimeName } ` )
98+ return
99+ }
106100
107- const cwd = resolve ( playgroundDir , `../playground-${ runtimeName } - ${ socketsEnabled ? 'sockets' : 'nosockets' } ` )
101+ const cwd = resolve ( playgroundDir , `../playground-${ runtimeName } ` )
108102
109- afterAll ( async ( ) => {
110- await server ?. close ( )
111- await rm ( cwd , { recursive : true , force : true } ) . catch ( ( ) => null )
112- } )
103+ afterAll ( async ( ) => {
104+ await server ?. close ( )
105+ await rm ( cwd , { recursive : true , force : true } ) . catch ( ( ) => null )
106+ } )
113107
114- const it = createIt ( runtimeName , socketsEnabled )
108+ const it = createIt ( runtimeName )
115109
116- it ( 'should start dev server' , { timeout : isCI ? 120_000 : 30_000 } , async ( ) => {
117- rmSync ( cwd , { recursive : true , force : true } )
118- cpSync ( playgroundDir , cwd , {
119- recursive : true ,
120- filter : src => ! src . includes ( '.nuxt' ) && ! src . includes ( '.output' ) && ! src . includes ( 'node_modules' ) ,
121- } )
122- server = await startDevServer ( {
123- cwd,
124- runtime : runtimeName ,
125- socketsEnabled,
126- } )
110+ it ( 'should start dev server' , { timeout : isCI ? 120_000 : 30_000 } , async ( ) => {
111+ rmSync ( cwd , { recursive : true , force : true } )
112+ cpSync ( playgroundDir , cwd , {
113+ recursive : true ,
114+ filter : src => ! src . includes ( '.nuxt' ) && ! src . includes ( '.output' ) && ! src . includes ( 'node_modules' ) ,
127115 } )
128-
129- it ( 'should serve the main page' , async ( ) => {
130- const response = await fetch ( server . url )
131- expect ( response . status ) . toBe ( 200 )
132-
133- const html = await response . text ( )
134- expect ( html ) . toContain ( 'Welcome to the Nuxt CLI playground' )
135- expect ( html ) . toContain ( '<!DOCTYPE html>' )
116+ server = await startDevServer ( {
117+ cwd,
118+ runtime : runtimeName ,
136119 } )
120+ } )
137121
138- it ( 'should serve static assets' , async ( ) => {
139- const response = await fetch ( `${ server . url } /favicon.ico` )
140- expect ( response . status ) . toBe ( 200 )
141- expect ( response . headers . get ( 'content-type' ) ) . toContain ( 'image/' )
142- } )
122+ it ( 'should serve the main page' , async ( ) => {
123+ const response = await fetch ( server . url )
124+ expect ( response . status ) . toBe ( 200 )
143125
144- it ( 'should handle API routes' , async ( ) => {
145- const response = await fetch ( ` ${ server . url } /api/hello` )
146- expect ( response . status ) . toBe ( 200 )
147- } )
126+ const html = await response . text ( )
127+ expect ( html ) . toContain ( 'Welcome to the Nuxt CLI playground' )
128+ expect ( html ) . toContain ( '<!DOCTYPE html>' )
129+ } )
148130
149- it ( 'should handle POST requests' , async ( ) => {
150- const response = await fetch ( `${ server . url } /api/echo` , {
151- method : 'POST' ,
152- headers : { 'Content-Type' : 'application/json' } ,
153- body : JSON . stringify ( { test : 'data' } ) ,
154- } )
131+ it ( 'should serve static assets' , async ( ) => {
132+ const response = await fetch ( `${ server . url } /favicon.ico` )
133+ expect ( response . status ) . toBe ( 200 )
134+ expect ( response . headers . get ( 'content-type' ) ) . toContain ( 'image/' )
135+ } )
155136
156- expect ( response . status ) . toBe ( 200 )
137+ it ( 'should handle API routes' , async ( ) => {
138+ const response = await fetch ( `${ server . url } /api/hello` )
139+ expect ( response . status ) . toBe ( 200 )
140+ } )
141+
142+ it ( 'should handle POST requests' , async ( ) => {
143+ const response = await fetch ( `${ server . url } /api/echo` , {
144+ method : 'POST' ,
145+ headers : { 'Content-Type' : 'application/json' } ,
146+ body : JSON . stringify ( { test : 'data' } ) ,
157147 } )
158148
159- it ( 'should preserve request headers' , async ( ) => {
160- const headers = {
161- 'X-Custom-Header' : 'test-value' ,
162- 'User-Agent' : 'vitest' ,
163- }
149+ expect ( response . status ) . toBe ( 200 )
150+ } )
164151
165- const res = await fetch ( `${ server . url } /api/echo` , { headers } )
166- const { headers : receivedHeaders } = await res . json ( )
152+ it ( 'should preserve request headers' , async ( ) => {
153+ const headers = {
154+ 'X-Custom-Header' : 'test-value' ,
155+ 'User-Agent' : 'vitest' ,
156+ }
167157
168- expect ( receivedHeaders ) . toMatchObject ( {
169- 'user-agent' : 'vitest' ,
170- 'x-custom-header' : 'test-value' ,
171- } )
158+ const res = await fetch ( `${ server . url } /api/echo` , { headers } )
159+ const { headers : receivedHeaders } = await res . json ( )
172160
173- expect ( res . status ) . toBe ( 200 )
161+ expect ( receivedHeaders ) . toMatchObject ( {
162+ 'user-agent' : 'vitest' ,
163+ 'x-custom-header' : 'test-value' ,
174164 } )
175165
176- it ( 'should handle concurrent requests' , async ( ) => {
177- const requests = Array . from ( { length : 5 } , ( ) => fetch ( server . url ) )
178- const responses = await Promise . all ( requests )
166+ expect ( res . status ) . toBe ( 200 )
167+ } )
179168
180- for ( const response of responses ) {
181- expect ( response . status ) . toBe ( 200 )
182- expect ( await response . text ( ) ) . toContain ( 'Welcome to the Nuxt CLI playground' )
183- }
169+ it ( 'should handle concurrent requests' , async ( ) => {
170+ const requests = Array . from ( { length : 5 } , ( ) => fetch ( server . url ) )
171+ const responses = await Promise . all ( requests )
172+
173+ for ( const response of responses ) {
174+ expect ( response . status ) . toBe ( 200 )
175+ expect ( await response . text ( ) ) . toContain ( 'Welcome to the Nuxt CLI playground' )
176+ }
177+ } )
178+
179+ it ( 'should handle large request payloads' , async ( ) => {
180+ const largePayload = { data : 'x' . repeat ( 10_000 ) }
181+ const response = await fetch ( `${ server . url } /api/echo` , {
182+ method : 'POST' ,
183+ headers : { 'Content-Type' : 'application/json' } ,
184+ body : JSON . stringify ( largePayload ) ,
184185 } )
185186
186- it ( 'should handle large request payloads' , async ( ) => {
187- const largePayload = { data : 'x' . repeat ( 10_000 ) }
188- const response = await fetch ( ` ${ server . url } /api/echo` , {
189- method : 'POST' ,
190- headers : { 'Content-Type' : 'application/json' } ,
191- body : JSON . stringify ( largePayload ) ,
192- } )
187+ expect ( response . status ) . toBe ( 200 )
188+ const result = await response . json ( )
189+ expect ( result . echoed . data ) . toBe ( largePayload . data )
190+ } )
191+
192+ it ( 'should handle different HTTP methods' , async ( ) => {
193+ const methods = [ 'GET' , 'POST' , 'PUT' , 'DELETE' ]
193194
195+ for ( const method of methods ) {
196+ const response = await fetch ( `${ server . url } /api/hello` , { method } )
194197 expect ( response . status ) . toBe ( 200 )
198+
195199 const result = await response . json ( )
196- expect ( result . echoed . data ) . toBe ( largePayload . data )
197- } )
200+ expect ( result . method ) . toBe ( method )
201+ }
202+ } )
198203
199- it ( 'should handle different HTTP methods ' , async ( ) => {
200- const methods = [ 'GET ', 'POST' , 'PUT' , 'DELETE' ]
204+ it ( 'should establish websocket connection and handle ping/pong ' , async ( ) => {
205+ const wsUrl = ` ${ server . url . replace ( 'http ', 'ws' ) } /_ws`
201206
202- for ( const method of methods ) {
203- const response = await fetch ( `${ server . url } /api/hello` , { method } )
204- expect ( response . status ) . toBe ( 200 )
207+ let isConnected = false
208+ let receivedPong = false
205209
206- const result = await response . json ( )
207- expect ( result . method ) . toBe ( method )
208- }
210+ await createWebSocketTest ( {
211+ url : wsUrl ,
212+ timeout : 2_000 ,
213+ testId : 'ping/pong' ,
214+ onOpen : ( ws ) => {
215+ isConnected = true
216+ ws . send ( 'ping test message' )
217+ } ,
218+ onMessage : ( ws , event ) => {
219+ const message = event . data . toString ( )
220+ if ( message === 'pong' ) {
221+ receivedPong = true
222+ ws . close ( )
223+ }
224+ } ,
225+ onClose : ( ) => isConnected && receivedPong ,
209226 } )
227+ } )
210228
211- it ( 'should establish websocket connection and handle ping/pong' , async ( ) => {
212- const wsUrl = `${ server . url . replace ( 'http' , 'ws' ) } /_ws`
229+ it ( 'should handle multiple concurrent websocket connections' , async ( ) => {
230+ const wsUrl = `${ server . url . replace ( 'http' , 'ws' ) } /_ws`
231+ const connectionCount = 2
213232
214- let isConnected = false
233+ const connectionPromises = Array . from ( { length : connectionCount } , ( _ , index ) => {
215234 let receivedPong = false
216235
217- await createWebSocketTest ( {
236+ return createWebSocketTest ( {
218237 url : wsUrl ,
219238 timeout : 2_000 ,
220- testId : 'ping/pong' ,
239+ testId : `concurrent connection ${ index } ` ,
221240 onOpen : ( ws ) => {
222- isConnected = true
223- ws . send ( 'ping test message' )
241+ ws . send ( `ping from connection ${ index } ` )
224242 } ,
225243 onMessage : ( ws , event ) => {
226- const message = event . data . toString ( )
227- if ( message === 'pong' ) {
244+ if ( event . data . toString ( ) === 'pong' ) {
228245 receivedPong = true
229246 ws . close ( )
230247 }
231248 } ,
232- onClose : ( ) => isConnected && receivedPong ,
249+ onClose : ( ) => receivedPong ,
233250 } )
234251 } )
235252
236- it ( 'should handle multiple concurrent websocket connections' , async ( ) => {
237- const wsUrl = `${ server . url . replace ( 'http' , 'ws' ) } /_ws`
238- const connectionCount = 2
239-
240- const connectionPromises = Array . from ( { length : connectionCount } , ( _ , index ) => {
241- let receivedPong = false
242-
243- return createWebSocketTest ( {
244- url : wsUrl ,
245- timeout : 2_000 ,
246- testId : `concurrent connection ${ index } ` ,
247- onOpen : ( ws ) => {
248- ws . send ( `ping from connection ${ index } ` )
249- } ,
250- onMessage : ( ws , event ) => {
251- if ( event . data . toString ( ) === 'pong' ) {
252- receivedPong = true
253- ws . close ( )
254- }
255- } ,
256- onClose : ( ) => receivedPong ,
257- } )
258- } )
259-
260- await Promise . all ( connectionPromises )
261- } )
253+ await Promise . all ( connectionPromises )
262254 } )
263255} )
264256
@@ -274,9 +266,8 @@ async function startDevServer(options: {
274266 port ?: number
275267 runtime ?: 'node' | 'bun' | 'deno'
276268 env ?: Record < string , string >
277- socketsEnabled ?: boolean
278269} ) : Promise < DevServerInstance > {
279- const { cwd, port : preferredPort , runtime = 'node' , env = { } , socketsEnabled = true } = options
270+ const { cwd, port : preferredPort , runtime = 'node' , env = { } } = options
280271 const port = preferredPort || await getPort ( { port : 3100 } )
281272 const host = '127.0.0.1'
282273 const url = `http://${ host } :${ port } `
@@ -305,7 +296,6 @@ async function startDevServer(options: {
305296 NUXT_TELEMETRY_DISABLED : '1' ,
306297 PORT : String ( port ) ,
307298 HOST : host ,
308- NUXT_SOCKET : socketsEnabled ? '1' : '0' ,
309299 } ,
310300 } )
311301
0 commit comments