@@ -139,17 +139,29 @@ describe('dev server', () => {
139139 const reader = response . body ! . getReader ( )
140140 const decoder = new TextDecoder ( )
141141 let stream = ''
142- while ( ! stream . includes ( 'event: nuxt:ready' ) ) {
143- const { value, done } = await reader . read ( )
144- if ( done ) {
145- break
142+ async function readUntil ( marker : string ) : Promise < void > {
143+ while ( ! stream . includes ( marker ) ) {
144+ const { value, done } = await reader . read ( )
145+ if ( done ) {
146+ return
147+ }
148+ stream += decoder . decode ( value )
146149 }
147- stream += decoder . decode ( value )
148150 }
149- await reader . cancel ( )
151+ // Only whole events, so a half-read chunk cannot be parsed as a snapshot.
152+ const latest = ( ) => JSON . parse ( [ ...stream . matchAll ( / d a t a : ( .+ ) \n / g) ] . pop ( ) ! [ 1 ] ! )
150153
154+ await readUntil ( 'event: nuxt:ready' )
151155 expect ( stream ) . toContain ( 'event: nuxt:ready' )
152- expect ( JSON . parse ( stream . split ( 'data: ' ) . pop ( ) ! ) ) . toMatchObject ( { status : 'ready' , progress : 1 } )
156+ // Whoever is streaming this is waiting for a page, and being ready means
157+ // the server can accept that request rather than that it has answered it.
158+ expect ( latest ( ) ) . toMatchObject ( { status : 'ready' , serving : false } )
159+
160+ await fetch ( `http://${ host } :${ port } /` , { headers : { accept : 'text/html' } } )
161+ await readUntil ( '"serving":true' )
162+ await reader . cancel ( )
163+
164+ expect ( latest ( ) ) . toMatchObject ( { status : 'ready' , serving : true , progress : 1 } )
153165 }
154166 finally {
155167 await close ( )
0 commit comments