(maxAttempts = 30)
| 181 | * Wait for server to be available |
| 182 | */ |
| 183 | export async function waitForServer(maxAttempts = 30): Promise<void> { |
| 184 | for (let i = 0; i < maxAttempts; i++) { |
| 185 | try { |
| 186 | await new Promise<void>((resolve, reject) => { |
| 187 | const req = http.get(`http://localhost:${state.serverPort}/api/health`, (res) => { |
| 188 | if (res.statusCode === 200) { |
| 189 | resolve(); |
| 190 | } else { |
| 191 | reject(new Error(`Status: ${res.statusCode}`)); |
| 192 | } |
| 193 | }); |
| 194 | req.on('error', reject); |
| 195 | req.setTimeout(1000, () => { |
| 196 | req.destroy(); |
| 197 | reject(new Error('Timeout')); |
| 198 | }); |
| 199 | }); |
| 200 | logger.info('Server is ready'); |
| 201 | return; |
| 202 | } catch { |
| 203 | await new Promise((r) => setTimeout(r, 500)); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | throw new Error('Server failed to start'); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Stop the backend server if running |
no test coverage detected