@@ -243,6 +243,65 @@ describe('Instabug Utils', () => {
243243 NonFatalErrorLevel . error ,
244244 ) ;
245245 } ) ;
246+ it ( 'getCrashDataFromError should include one level of cause crash' , ( ) => {
247+ const causeError = new TypeError ( 'Cause error' ) ;
248+ const rootError = new Error ( 'Root error' ) ;
249+ ( rootError as any ) . cause = causeError ;
250+
251+ const crashData = InstabugUtils . getCrashDataFromError ( rootError ) ;
252+ const jsStackTraceRootError = InstabugUtils . getStackTrace ( rootError ) ;
253+ const jsStackTraceCauseError = InstabugUtils . getStackTrace ( causeError ) ;
254+
255+ expect ( crashData . message ) . toBe ( 'Error - Root error' ) ;
256+ expect ( crashData . e_message ) . toBe ( 'Root error' ) ;
257+ expect ( crashData . e_name ) . toBe ( 'Error' ) ;
258+ expect ( crashData . platform ) . toBe ( 'react_native' ) ;
259+ expect ( crashData . exception ) . toEqual ( jsStackTraceRootError ) ;
260+ expect ( crashData . cause_crash ) . toBeDefined ( ) ;
261+ expect ( crashData . cause_crash ?. message ) . toBe ( 'TypeError - Cause error' ) ;
262+ expect ( crashData . cause_crash ?. e_name ) . toBe ( 'TypeError' ) ;
263+ expect ( crashData . cause_crash ?. exception ) . toEqual ( jsStackTraceCauseError ) ;
264+ } ) ;
265+
266+ it ( 'getCrashDataFromError should include up to 3 levels of cause crash' , ( ) => {
267+ const errorLevel3 = new Error ( 'Third level error' ) ;
268+ const errorLevel2 = new Error ( 'Second level error' ) ;
269+ const errorLevel1 = new Error ( 'First level error' ) ;
270+ const rootError = new Error ( 'Root error' ) ;
271+
272+ ( errorLevel2 as any ) . cause = errorLevel3 ;
273+ ( errorLevel1 as any ) . cause = errorLevel2 ;
274+ ( rootError as any ) . cause = errorLevel1 ;
275+
276+ const crashData = InstabugUtils . getCrashDataFromError ( rootError ) ;
277+
278+ expect ( crashData . message ) . toBe ( 'Error - Root error' ) ;
279+ expect ( crashData . cause_crash ?. message ) . toBe ( 'Error - First level error' ) ;
280+ expect ( crashData . cause_crash ?. cause_crash ?. message ) . toBe ( 'Error - Second level error' ) ;
281+ expect ( crashData . cause_crash ?. cause_crash ?. cause_crash ?. message ) . toBe (
282+ 'Error - Third level error' ,
283+ ) ;
284+ expect ( crashData . cause_crash ?. cause_crash ?. cause_crash ?. cause_crash ) . toBeUndefined ( ) ;
285+ } ) ;
286+
287+ it ( 'getCrashDataFromError should stop at 3 levels even if more causes exist' , ( ) => {
288+ const errorLevel4 = new Error ( 'Fourth level error' ) ;
289+ const errorLevel3 = new Error ( 'Third level error' ) ;
290+ const errorLevel2 = new Error ( 'Second level error' ) ;
291+ const errorLevel1 = new Error ( 'First level error' ) ;
292+ const rootError = new Error ( 'Root error' ) ;
293+
294+ ( errorLevel3 as any ) . cause = errorLevel4 ;
295+ ( errorLevel2 as any ) . cause = errorLevel3 ;
296+ ( errorLevel1 as any ) . cause = errorLevel2 ;
297+ ( rootError as any ) . cause = errorLevel1 ;
298+
299+ const crashData = InstabugUtils . getCrashDataFromError ( rootError ) ;
300+
301+ const thirdLevel = crashData . cause_crash ?. cause_crash ?. cause_crash ;
302+ expect ( thirdLevel ?. message ) . toBe ( 'Error - Third level error' ) ;
303+ expect ( thirdLevel ?. cause_crash ) . toBeUndefined ( ) ; // should not include 4th level
304+ } ) ;
246305} ) ;
247306
248307describe ( 'reportNetworkLog' , ( ) => {
0 commit comments