* Flushes all already queued promises and returns a promise. Note this will * only work if the promises have already been queued, so it cannot be used to * wait on a promise that hasn't been dispatched yet.
()
| 35 | * wait on a promise that hasn't been dispatched yet. |
| 36 | */ |
| 37 | public static async flushPromises(): Promise<void> { |
| 38 | const testApi = getTestApi(); |
| 39 | if ( |
| 40 | typeof testApi.isMockFunction === 'function' && |
| 41 | testApi.isMockFunction(setTimeout) && |
| 42 | typeof testApi.advanceTimersByTime === 'function' |
| 43 | ) { |
| 44 | // Fake timers won't advance unless we flush them explicitly. |
| 45 | testApi.advanceTimersByTime(0); |
| 46 | await Promise.resolve(); |
| 47 | testApi.advanceTimersByTime(0); |
| 48 | await Promise.resolve(); |
| 49 | return; |
| 50 | } |
| 51 | await new Promise(resolve => setTimeout(resolve, 0)); |
| 52 | await new Promise(resolve => setTimeout(resolve, 0)); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Adds a one-time mock implementation to the provided spy that mimics an error |