(
assertion: () => void | Promise<void>,
{ timeout = 2000, interval = 50 } = {},
)
| 13 | // The version of waitFor from vitest is still fine to use if you aren't waiting |
| 14 | // for React state updates. |
| 15 | export async function waitFor( |
| 16 | assertion: () => void | Promise<void>, |
| 17 | { timeout = 2000, interval = 50 } = {}, |
| 18 | ): Promise<void> { |
| 19 | const startTime = Date.now(); |
| 20 | |
| 21 | while (true) { |
| 22 | try { |
| 23 | await assertion(); |
| 24 | return; |
| 25 | } catch (error) { |
| 26 | if (Date.now() - startTime > timeout) { |
| 27 | throw error; |
| 28 | } |
| 29 | |
| 30 | await act(async () => { |
| 31 | if (vi.isFakeTimers()) { |
| 32 | await vi.advanceTimersByTimeAsync(interval); |
| 33 | } else { |
| 34 | await new Promise((resolve) => setTimeout(resolve, interval)); |
| 35 | } |
| 36 | }); |
| 37 | } |
| 38 | } |
| 39 | } |
no outgoing calls