(serverProcess, port, timeout)
| 115 | |
| 116 | // helper to validate that server was started on the correct port |
| 117 | async function startedOnPort(serverProcess, port, timeout) { |
| 118 | let checkPort |
| 119 | const startedPromise = new Promise<void>((resolve, reject) => { |
| 120 | checkPort = (data) => { |
| 121 | const str = stripVTControlCharacters(data.toString()) |
| 122 | const match = str.match( |
| 123 | /http:\/\/(?:localhost|127\.0\.0\.1|\[::1\]):(\d{4})/, |
| 124 | ) |
| 125 | if (match) { |
| 126 | const startedPort = parseInt(match[1], 10) |
| 127 | if (startedPort === port) { |
| 128 | resolve() |
| 129 | } else { |
| 130 | const msg = `server listens on port ${startedPort} instead of ${port}` |
| 131 | reject(msg) |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | serverProcess.stdout.on('data', checkPort) |
| 136 | }) |
| 137 | return resolvedOrTimeout( |
| 138 | startedPromise, |
| 139 | timeout, |
| 140 | `failed to start within ${timeout}ms`, |
| 141 | ).finally(() => serverProcess.stdout.off('data', checkPort)) |
| 142 | } |
| 143 | |
| 144 | // helper function that rejects with errorMessage if promise isn't settled within ms |
| 145 | async function resolvedOrTimeout(promise, ms, errorMessage) { |
no test coverage detected