| 41 | } |
| 42 | |
| 43 | export async function waitFor(expectedLog, options) { |
| 44 | assertYieldsWereCleared(waitFor); |
| 45 | |
| 46 | class="cm">// Create the error object before doing any async work, to get a better |
| 47 | class="cm">// stack trace. |
| 48 | const error = new Error(); |
| 49 | Error.captureStackTrace(error, waitFor); |
| 50 | |
| 51 | const stopAfter = expectedLog.length; |
| 52 | const actualLog = []; |
| 53 | do { |
| 54 | class="cm">// Wait until end of current task/microtask. |
| 55 | await waitForMicrotasks(); |
| 56 | if (SchedulerMock.unstable_hasPendingWork()) { |
| 57 | SchedulerMock.unstable_flushNumberOfYields(stopAfter - actualLog.length); |
| 58 | actualLog.push(...SchedulerMock.unstable_clearLog()); |
| 59 | if (stopAfter > actualLog.length) { |
| 60 | class="cm">// Continue flushing until we've logged the expected number of items. |
| 61 | } else { |
| 62 | class="cm">// Once we've reached the expected sequence, wait one more microtask to |
| 63 | class="cm">// flush any remaining synchronous work. |
| 64 | await waitForMicrotasks(); |
| 65 | actualLog.push(...SchedulerMock.unstable_clearLog()); |
| 66 | break; |
| 67 | } |
| 68 | } else { |
| 69 | class="cm">// There's no pending work, even after a microtask. |
| 70 | break; |
| 71 | } |
| 72 | } while (true); |
| 73 | |
| 74 | if (options && options.additionalLogsAfterAttemptingToYield) { |
| 75 | expectedLog = expectedLog.concat( |
| 76 | options.additionalLogsAfterAttemptingToYield, |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | if (equals(actualLog, expectedLog)) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | error.message = ` |
| 85 | Expected sequence of events did not occur. |
| 86 | |
| 87 | ${diff(expectedLog, actualLog)} |
| 88 | `; |
| 89 | throw error; |
| 90 | } |
| 91 | |
| 92 | export async function waitForAll(expectedLog) { |
| 93 | assertYieldsWereCleared(waitForAll); |