@@ -640,3 +640,92 @@ describe.each([
640640 ) ;
641641 } ) ;
642642} ) ;
643+
644+ it ( 'M intercept and send a RUM event W on error() {called from RNErrorHandler}' , async ( ) => {
645+ // GIVEN
646+ const errorHandlerMock = new RNErrorHandlerMock ( ) ;
647+ DdRumErrorTracking . startTracking ( ) ;
648+ const is_fatal = Math . random ( ) < 0.5 ;
649+ const error = new Error ( 'Something bad happened' ) ;
650+
651+ // WHEN
652+ errorHandlerMock . onError ( error , is_fatal ) ;
653+ await flushPromises ( ) ;
654+
655+ // THEN
656+ expect ( DdRum . addError ) . toHaveBeenCalledTimes ( 1 ) ;
657+ expect ( DdRum . addError ) . toHaveBeenCalledWith (
658+ 'Something bad happened' ,
659+ 'SOURCE' ,
660+ expect . stringContaining ( 'Error: Something bad happened' ) ,
661+ {
662+ '_dd.error.raw' : error ,
663+ '_dd.error.is_crash' : is_fatal ,
664+ '_dd.error.source_type' : 'react-native'
665+ } ,
666+ expect . any ( Number )
667+ ) ;
668+ expect ( DdRum . addError . mock . calls [ 0 ] [ 2 ] ) . toContain (
669+ '/packages/core/src/__tests__/rum/instrumentation/DdRumErrorTracking.test.tsx'
670+ ) ;
671+ expect ( baseErrorHandlerCalled ) . toStrictEqual ( true ) ;
672+ expect ( DdLogs . errorWithError ) . toHaveBeenCalledTimes ( 1 ) ;
673+ expect ( DdLogs . errorWithError ) . toHaveBeenCalledWith (
674+ 'Something bad happened' ,
675+ 'Error' ,
676+ 'Something bad happened' ,
677+ expect . stringContaining ( 'Error: Something bad happened' ) ,
678+ {
679+ '_dd.error.raw' : error ,
680+ '_dd.error.is_crash' : is_fatal ,
681+ '_dd.error.source_type' : 'react-native'
682+ }
683+ ) ;
684+ } ) ;
685+
686+ it ( 'M intercept and send a RUM event W onConsole() {called from RNErrorHandler}' , async ( ) => {
687+ // GIVEN
688+ const errorHandlerMock = new RNErrorHandlerMock ( ) ;
689+ DdRumErrorTracking . startTracking ( ) ;
690+ const message = 'Oops I did it again!' ;
691+
692+ // WHEN
693+ errorHandlerMock . onConsoleError ( message ) ;
694+ await flushPromises ( ) ;
695+
696+ // THEN
697+ expect ( DdRum . addError ) . toHaveBeenCalledTimes ( 1 ) ;
698+ expect ( DdRum . addError ) . toHaveBeenCalledWith (
699+ 'Oops I did it again!' ,
700+ 'CONSOLE' ,
701+ '' ,
702+ {
703+ '_dd.error.source_type' : 'react-native'
704+ } ,
705+ expect . any ( Number )
706+ ) ;
707+ expect ( baseConsoleErrorCalled ) . toStrictEqual ( true ) ;
708+ expect ( DdLogs . errorWithError ) . toHaveBeenCalledTimes ( 1 ) ;
709+ expect ( DdLogs . errorWithError ) . toHaveBeenCalledWith (
710+ 'Oops I did it again!' ,
711+ 'Error' ,
712+ 'Oops I did it again!' ,
713+ '' ,
714+ { '_dd.error.source_type' : 'react-native' }
715+ ) ;
716+ } ) ;
717+
718+ /**
719+ * This is a mock of the RN error handler class that will call the ErrorUtils.
720+ * Testing with this catches bugs around `this` references.
721+ */
722+ class RNErrorHandlerMock {
723+ onError = ( error : any , isFatal : boolean ) => {
724+ const errorHandler = ErrorUtils . getGlobalHandler ( ) ;
725+ errorHandler ( error , isFatal ) ;
726+ } ;
727+
728+ onConsoleError = ( ...params : any ) => {
729+ console . error ( ...params ) ;
730+ } ;
731+ }
0 commit comments