| 129 | } |
| 130 | |
| 131 | export async function waitForThrow(expectedError: mixed): mixed { |
| 132 | assertYieldsWereCleared(waitForThrow); |
| 133 | |
| 134 | // Create the error object before doing any async work, to get a better |
| 135 | // stack trace. |
| 136 | const error = new Error(); |
| 137 | Error.captureStackTrace(error, waitForThrow); |
| 138 | |
| 139 | do { |
| 140 | // Wait until end of current task/microtask. |
| 141 | await waitForMicrotasks(); |
| 142 | if (!SchedulerMock.unstable_hasPendingWork()) { |
| 143 | // There's no pending work, even after a microtask. Stop flushing. |
| 144 | error.message = 'Expected something to throw, but nothing did.'; |
| 145 | throw error; |
| 146 | } |
| 147 | |
| 148 | const errorHandlerDOM = function (event: ErrorEvent) { |
| 149 | // Prevent logs from reprinting this error. |
| 150 | event.preventDefault(); |
| 151 | thrownErrors.push(event.error); |
| 152 | }; |
| 153 | const errorHandlerNode = function (err: mixed) { |
| 154 | thrownErrors.push(err); |
| 155 | }; |
| 156 | // We track errors that were logged globally as if they occurred in this scope and then rethrow them. |
| 157 | if (actingUpdatesScopeDepth === 0) { |
| 158 | if ( |
| 159 | typeof window === 'object' && |
| 160 | typeof window.addEventListener === 'function' |
| 161 | ) { |
| 162 | // We're in a JS DOM environment. |
| 163 | window.addEventListener('error', errorHandlerDOM); |
| 164 | } else if (typeof process === 'object') { |
| 165 | // Node environment |
| 166 | process.on('uncaughtException', errorHandlerNode); |
| 167 | } |
| 168 | } |
| 169 | try { |
| 170 | SchedulerMock.unstable_flushAllWithoutAsserting(); |
| 171 | } catch (x) { |
| 172 | thrownErrors.push(x); |
| 173 | } finally { |
| 174 | if (actingUpdatesScopeDepth === 0) { |
| 175 | if ( |
| 176 | typeof window === 'object' && |
| 177 | typeof window.addEventListener === 'function' |
| 178 | ) { |
| 179 | // We're in a JS DOM environment. |
| 180 | window.removeEventListener('error', errorHandlerDOM); |
| 181 | } else if (typeof process === 'object') { |
| 182 | // Node environment |
| 183 | process.off('uncaughtException', errorHandlerNode); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | if (thrownErrors.length > 0) { |
| 188 | const thrownError = aggregateErrors(thrownErrors); |