@@ -208,17 +208,56 @@ export const toThrowMatcher: AsyncMatcher<ToThrowArgs> = async (
208208 let thrownException = false ;
209209 let thrownExceptionMessage = null ;
210210
211+ // Errors thrown on a Worklet Runtime inside a synchronous call (e.g.
212+ // `runOnUISync`) don't reject on the RN runtime — they are reported
213+ // asynchronously through `ErrorUtils.reportFatalError`. Capture the global
214+ // handler for the duration of the call so the matcher sees them regardless
215+ // of which handler (LogBox, remote reporter) is installed around the run.
216+ const errorUtils = (
217+ globalThis as {
218+ ErrorUtils ?: {
219+ getGlobalHandler : ( ) => ( error : Error , isFatal ?: boolean ) => void ;
220+ setGlobalHandler : (
221+ handler : ( error : Error , isFatal ?: boolean ) => void
222+ ) => void ;
223+ } ;
224+ }
225+ ) . ErrorUtils ;
226+ let uncaughtErrorMessage : string | null = null ;
227+ const previousGlobalHandler = errorUtils ?. getGlobalHandler ( ) ;
228+ errorUtils ?. setGlobalHandler ( ( error ) => {
229+ if ( uncaughtErrorMessage === null ) {
230+ uncaughtErrorMessage = error ?. message ?? String ( error ) ;
231+ }
232+ } ) ;
233+
211234 try {
212235 await throwingFunction ( ) ;
213236 } catch ( e ) {
214237 thrownException = true ;
215238 thrownExceptionMessage = ( e as Error ) ?. message || '' ;
216239 }
240+
241+ const deadline = Date . now ( ) + 500 ;
242+ while (
243+ ! thrownException &&
244+ uncaughtErrorMessage === null &&
245+ getCapturedConsoleErrors ( ) . consoleErrorCount < 1 &&
246+ Date . now ( ) < deadline
247+ ) {
248+ await new Promise ( ( resolve ) => setTimeout ( resolve , 20 ) ) ;
249+ }
250+
251+ if ( previousGlobalHandler ) {
252+ errorUtils ?. setGlobalHandler ( previousGlobalHandler ) ;
253+ }
217254 await restoreConsole ( ) ;
218255
219256 const { consoleErrorCount, consoleErrorMessage } = getCapturedConsoleErrors ( ) ;
220- const errorWasThrown = thrownException || consoleErrorCount >= 1 ;
221- const capturedMessage = thrownExceptionMessage || consoleErrorMessage ;
257+ const errorWasThrown =
258+ thrownException || uncaughtErrorMessage !== null || consoleErrorCount >= 1 ;
259+ const capturedMessage =
260+ thrownExceptionMessage || uncaughtErrorMessage || consoleErrorMessage ;
222261 const messageIsCorrect = errorMessage
223262 ? capturedMessage . includes ( errorMessage )
224263 : true ;
0 commit comments