* Helper function to wait for a condition to become true by polling * @param condition - A function that returns true when the condition is met * @param timeout - Maximum time to wait in milliseconds (default: 5000) * @param pollInterval - Interval between checks in milliseconds (default: 10) *
( condition: () => boolean, timeout = 5000, pollInterval = 10, )
| 32 | * @throws {Error} If the timeout elapses before the condition becomes true |
| 33 | */ |
| 34 | async function waitFor( |
| 35 | condition: () => boolean, |
| 36 | timeout = 5000, |
| 37 | pollInterval = 10, |
| 38 | ): Promise<void> { |
| 39 | const startTime = Date.now(); |
| 40 | while (!condition() && Date.now() - startTime < timeout) { |
| 41 | await new Promise((resolve) => setTimeout(resolve, pollInterval)); |
| 42 | } |
| 43 | if (!condition()) { |
| 44 | throw new Error( |
| 45 | `waitFor: timeout waiting for condition after ${timeout}ms`, |
| 46 | ); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | describe("Setup -> apiSetup", () => { |
| 51 | const originalEnv = { ...process.env }; |