@@ -29,44 +29,57 @@ let baseErrorHandler = (error: any, isFatal?: boolean) => {
2929} ;
3030let originalErrorHandler = undefined ;
3131
32+ let baseConsoleErrorCalled = false ;
33+ let baseConsoleError = ( ...params : unknown ) => {
34+ baseConsoleErrorCalled = true
35+ }
36+ let originalConsoleError = undefined
37+
3238const flushPromises = ( ) => new Promise ( setImmediate ) ;
3339
3440beforeEach ( ( ) => {
3541 DdRum . addError . mockClear ( ) ;
3642 baseErrorHandlerCalled = false ;
3743 originalErrorHandler = ErrorUtils . getGlobalHandler ( ) ;
3844 ErrorUtils . setGlobalHandler ( baseErrorHandler ) ;
45+ originalConsoleError = console . error ;
46+ console . error = baseConsoleError ;
3947 jest . setTimeout ( 20000 )
4048} )
4149
4250afterEach ( ( ) => {
4351 DdRumErrorTracking [ 'isTracking' ] = false
4452 ErrorUtils . setGlobalHandler ( originalErrorHandler )
53+ console . error = originalConsoleError
4554} )
4655
4756
48- it ( 'M intercept and send a RUM event W onError () {empty stack trace}' , async ( ) => {
57+ it ( 'M intercept and send a RUM event W onGlobalError () {empty stack trace}' , async ( ) => {
4958 // GIVEN
50- DdRumErrorTracking . startTracking ( )
59+ DdRumErrorTracking . startTracking ( ) ;
60+ const is_fatal = Math . random ( ) < 0.5 ;
5161 const error = new Error ( 'Something bad happened' ) ;
5262
5363 // WHEN
54- DdRumErrorTracking . onError ( error , false ) ;
64+ DdRumErrorTracking . onGlobalError ( error , is_fatal ) ;
5565 await flushPromises ( ) ;
5666
5767 // THEN
5868 expect ( DdRum . addError . mock . calls . length ) . toBe ( 1 ) ;
5969 expect ( DdRum . addError . mock . calls [ 0 ] [ 0 ] ) . toBe ( String ( error ) ) ;
6070 expect ( DdRum . addError . mock . calls [ 0 ] [ 1 ] ) . toBe ( "SOURCE" ) ;
6171 expect ( DdRum . addError . mock . calls [ 0 ] [ 2 ] ) . toBe ( "" ) ;
62- expect ( DdRum . addError . mock . calls [ 0 ] [ 4 ] ) . toStrictEqual ( error ) ;
72+ const attributes = DdRum . addError . mock . calls [ 0 ] [ 4 ] ;
73+ expect ( attributes [ "_dd.error.raw" ] ) . toStrictEqual ( error ) ;
74+ expect ( attributes [ "_dd.error.is_crash" ] ) . toStrictEqual ( is_fatal ) ;
6375 expect ( baseErrorHandlerCalled ) . toStrictEqual ( true ) ;
6476} )
6577
6678
67- it ( 'M intercept and send a RUM event W onError () {with source file info}' , async ( ) => {
79+ it ( 'M intercept and send a RUM event W onGlobalError () {with source file info}' , async ( ) => {
6880 // GIVEN
69- DdRumErrorTracking . startTracking ( )
81+ DdRumErrorTracking . startTracking ( ) ;
82+ const is_fatal = Math . random ( ) < 0.5 ;
7083 const error = {
7184 sourceURL : "./path/to/file.js" ,
7285 line : 1038 ,
@@ -75,37 +88,105 @@ it('M intercept and send a RUM event W onError() {with source file info}', async
7588 } ;
7689
7790 // WHEN
78- DdRumErrorTracking . onError ( error , false ) ;
91+ DdRumErrorTracking . onGlobalError ( error , is_fatal ) ;
7992 await flushPromises ( ) ;
8093
8194 // THEN
8295 expect ( DdRum . addError . mock . calls . length ) . toBe ( 1 ) ;
8396 expect ( DdRum . addError . mock . calls [ 0 ] [ 0 ] ) . toBe ( String ( error ) ) ;
8497 expect ( DdRum . addError . mock . calls [ 0 ] [ 1 ] ) . toBe ( "SOURCE" ) ;
8598 expect ( DdRum . addError . mock . calls [ 0 ] [ 2 ] ) . toBe ( "at ./path/to/file.js:1038:57" ) ;
86- expect ( DdRum . addError . mock . calls [ 0 ] [ 4 ] ) . toStrictEqual ( error ) ;
99+ const attributes = DdRum . addError . mock . calls [ 0 ] [ 4 ] ;
100+ expect ( attributes [ "_dd.error.raw" ] ) . toStrictEqual ( error ) ;
101+ expect ( attributes [ "_dd.error.is_crash" ] ) . toStrictEqual ( is_fatal ) ;
87102 expect ( baseErrorHandlerCalled ) . toStrictEqual ( true ) ;
88103} )
89104
90105
91- it ( 'M intercept and send a RUM event W onError () {with component stack}' , async ( ) => {
106+ it ( 'M intercept and send a RUM event W onGlobalError () {with component stack}' , async ( ) => {
92107 // GIVEN
93- DdRumErrorTracking . startTracking ( )
108+ DdRumErrorTracking . startTracking ( ) ;
109+ const is_fatal = Math . random ( ) < 0.5 ;
94110 const error = {
95111 componentStack : [ "doSomething() at ./path/to/file.js:67:3" , "nestedCall() at ./path/to/file.js:1064:9" , "root() at ./path/to/index.js:10:1" ] ,
96112 message : "Something bad happened"
97113 } ;
98114
99115 // WHEN
100- DdRumErrorTracking . onError ( error , false ) ;
116+ DdRumErrorTracking . onGlobalError ( error , is_fatal ) ;
101117 await flushPromises ( ) ;
102118
103119 // THEN
104120 expect ( DdRum . addError . mock . calls . length ) . toBe ( 1 ) ;
105121 expect ( DdRum . addError . mock . calls [ 0 ] [ 0 ] ) . toBe ( String ( error ) ) ;
106122 expect ( DdRum . addError . mock . calls [ 0 ] [ 1 ] ) . toBe ( "SOURCE" ) ;
107123 expect ( DdRum . addError . mock . calls [ 0 ] [ 2 ] ) . toBe ( "doSomething() at ./path/to/file.js:67:3,nestedCall() at ./path/to/file.js:1064:9,root() at ./path/to/index.js:10:1" ) ;
108- expect ( DdRum . addError . mock . calls [ 0 ] [ 4 ] ) . toStrictEqual ( error ) ;
124+ const attributes = DdRum . addError . mock . calls [ 0 ] [ 4 ] ;
125+ expect ( attributes [ "_dd.error.raw" ] ) . toStrictEqual ( error ) ;
126+ expect ( attributes [ "_dd.error.is_crash" ] ) . toStrictEqual ( is_fatal ) ;
109127 expect ( baseErrorHandlerCalled ) . toStrictEqual ( true ) ;
110128} )
111129
130+ it ( 'M intercept and send a RUM event W onConsole() {Error with source file info}' , async ( ) => {
131+ // GIVEN
132+ DdRumErrorTracking . startTracking ( ) ;
133+ const message = "Something bad happened" ;
134+ const error = {
135+ sourceURL : "./path/to/file.js" ,
136+ line : 1038 ,
137+ column : 57 ,
138+ message : message
139+ } ;
140+
141+ // WHEN
142+ DdRumErrorTracking . onConsoleError ( message , error ) ;
143+ await flushPromises ( ) ;
144+
145+ // THEN
146+ expect ( DdRum . addError . mock . calls . length ) . toBe ( 1 ) ;
147+ expect ( DdRum . addError . mock . calls [ 0 ] [ 0 ] ) . toBe ( message + " " + JSON . stringify ( error ) ) ;
148+ expect ( DdRum . addError . mock . calls [ 0 ] [ 1 ] ) . toBe ( "CONSOLE" ) ;
149+ expect ( DdRum . addError . mock . calls [ 0 ] [ 2 ] ) . toBe ( "at ./path/to/file.js:1038:57" ) ;
150+ expect ( DdRum . addError . mock . calls [ 0 ] [ 4 ] ) . toStrictEqual ( { } )
151+ expect ( baseConsoleErrorCalled ) . toStrictEqual ( true ) ;
152+ } )
153+
154+ it ( 'M intercept and send a RUM event W onConsole() {Error with component stack}' , async ( ) => {
155+ // GIVEN
156+ DdRumErrorTracking . startTracking ( ) ;
157+ const message = "Something bad happened" ;
158+ const error = {
159+ componentStack : [ "doSomething() at ./path/to/file.js:67:3" , "nestedCall() at ./path/to/file.js:1064:9" , "root() at ./path/to/index.js:10:1" ] ,
160+ message : "Something bad happened"
161+ } ;
162+
163+ // WHEN
164+ DdRumErrorTracking . onConsoleError ( message , error ) ;
165+ await flushPromises ( ) ;
166+
167+ // THEN
168+ expect ( DdRum . addError . mock . calls . length ) . toBe ( 1 ) ;
169+ expect ( DdRum . addError . mock . calls [ 0 ] [ 0 ] ) . toBe ( message + " " + JSON . stringify ( error ) ) ;
170+ expect ( DdRum . addError . mock . calls [ 0 ] [ 1 ] ) . toBe ( "CONSOLE" ) ;
171+ expect ( DdRum . addError . mock . calls [ 0 ] [ 2 ] ) . toBe ( "doSomething() at ./path/to/file.js:67:3,nestedCall() at ./path/to/file.js:1064:9,root() at ./path/to/index.js:10:1" ) ;
172+ expect ( DdRum . addError . mock . calls [ 0 ] [ 4 ] ) . toStrictEqual ( { } )
173+ expect ( baseConsoleErrorCalled ) . toStrictEqual ( true ) ;
174+ } )
175+
176+ it ( 'M intercept and send a RUM event W onConsole() {message only}' , async ( ) => {
177+ // GIVEN
178+ DdRumErrorTracking . startTracking ( ) ;
179+ const message = "Something bad happened" ;
180+
181+ // WHEN
182+ DdRumErrorTracking . onConsoleError ( message ) ;
183+ await flushPromises ( ) ;
184+
185+ // THEN
186+ expect ( DdRum . addError . mock . calls . length ) . toBe ( 1 ) ;
187+ expect ( DdRum . addError . mock . calls [ 0 ] [ 0 ] ) . toBe ( message ) ;
188+ expect ( DdRum . addError . mock . calls [ 0 ] [ 1 ] ) . toBe ( "CONSOLE" ) ;
189+ expect ( DdRum . addError . mock . calls [ 0 ] [ 2 ] ) . toBe ( "" ) ;
190+ expect ( DdRum . addError . mock . calls [ 0 ] [ 4 ] ) . toStrictEqual ( { } )
191+ expect ( baseConsoleErrorCalled ) . toStrictEqual ( true ) ;
192+ } )
0 commit comments