@@ -53,16 +53,47 @@ function waitForWs(baseUrl, timeoutMs = 60_000) {
5353 } ) ;
5454}
5555
56+ function warmUpPage ( baseUrl , timeoutMs = 30_000 ) {
57+ return new Promise ( ( resolve ) => {
58+ const deadline = Date . now ( ) + timeoutMs ;
59+ function attempt ( ) {
60+ if ( Date . now ( ) > deadline ) {
61+ resolve ( ) ;
62+ return ;
63+ }
64+ const url = new URL ( `${ baseUrl } /chats/main` ) ;
65+ const req = http . request (
66+ { hostname : url . hostname , port : url . port , path : url . pathname , timeout : 10000 } ,
67+ ( res ) => {
68+ // Consume the response body so the connection closes cleanly
69+ res . resume ( ) ;
70+ res . on ( "end" , ( ) => resolve ( ) ) ;
71+ } ,
72+ ) ;
73+ req . on ( "error" , ( ) => setTimeout ( attempt , 1000 ) ) ;
74+ req . on ( "timeout" , ( ) => {
75+ req . destroy ( ) ;
76+ setTimeout ( attempt , 1000 ) ;
77+ } ) ;
78+ req . end ( ) ;
79+ }
80+ attempt ( ) ;
81+ } ) ;
82+ }
83+
5684module . exports = async function globalSetup ( config ) {
57- // Wait for each webServer's WS endpoint to be ready
85+ const seen = new Set ( ) ;
5886 for ( const project of config . projects || [ ] ) {
5987 const baseURL = project . use ?. baseURL || config . use ?. baseURL ;
60- if ( baseURL ) {
61- try {
62- await waitForWs ( baseURL , 60_000 ) ;
63- } catch ( e ) {
64- console . warn ( `[global-setup] ${ e . message } ` ) ;
65- }
88+ if ( ! baseURL || seen . has ( baseURL ) ) continue ;
89+ seen . add ( baseURL ) ;
90+ try {
91+ // Wait for WS handler to be ready
92+ await waitForWs ( baseURL , 60_000 ) ;
93+ // Warm up the page-serving path (template compilation, asset loading)
94+ await warmUpPage ( baseURL , 30_000 ) ;
95+ } catch ( e ) {
96+ console . warn ( `[global-setup] ${ e . message } ` ) ;
6697 }
6798 }
6899} ;
0 commit comments