@@ -6,16 +6,16 @@ import { useState } from 'react';
6
6
7
7
import { ErrorBoundary , ErrorBoundaryProps , UNKNOWN_COMPONENT , withErrorBoundary } from '../src/errorboundary' ;
8
8
9
- const mockCaptureEvent = jest . fn ( ) ;
9
+ const mockCaptureException = jest . fn ( ) ;
10
10
const mockShowReportDialog = jest . fn ( ) ;
11
11
const EVENT_ID = 'test-id-123' ;
12
12
13
13
jest . mock ( '@sentry/browser' , ( ) => {
14
14
const actual = jest . requireActual ( '@sentry/browser' ) ;
15
15
return {
16
16
...actual ,
17
- captureEvent : ( event : Event ) => {
18
- mockCaptureEvent ( event ) ;
17
+ captureException : ( ... args : unknown [ ] ) => {
18
+ mockCaptureException ( ... args ) ;
19
19
return EVENT_ID ;
20
20
} ,
21
21
showReportDialog : ( options : any ) => {
@@ -74,7 +74,7 @@ describe('ErrorBoundary', () => {
74
74
jest . spyOn ( console , 'error' ) . mockImplementation ( ) ;
75
75
76
76
afterEach ( ( ) => {
77
- mockCaptureEvent . mockClear ( ) ;
77
+ mockCaptureException . mockClear ( ) ;
78
78
mockShowReportDialog . mockClear ( ) ;
79
79
} ) ;
80
80
@@ -220,60 +220,34 @@ describe('ErrorBoundary', () => {
220
220
) ;
221
221
222
222
expect ( mockOnError ) . toHaveBeenCalledTimes ( 0 ) ;
223
- expect ( mockCaptureEvent ) . toHaveBeenCalledTimes ( 0 ) ;
223
+ expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 0 ) ;
224
224
225
225
const btn = screen . getByTestId ( 'errorBtn' ) ;
226
226
fireEvent . click ( btn ) ;
227
227
228
228
expect ( mockOnError ) . toHaveBeenCalledTimes ( 1 ) ;
229
229
expect ( mockOnError ) . toHaveBeenCalledWith ( expect . any ( Error ) , expect . any ( String ) , expect . any ( String ) ) ;
230
230
231
- expect ( mockCaptureEvent ) . toHaveBeenCalledTimes ( 1 ) ;
232
-
233
- // We do a detailed assert on the stacktrace as a regression test against future
234
- // react changes (that way we can update the docs if frames change in a major way).
235
- const event = mockCaptureEvent . mock . calls [ 0 ] [ 0 ] ;
236
- expect ( event . exception . values ) . toHaveLength ( 2 ) ;
237
- expect ( event . level ) . toBe ( Severity . Error ) ;
238
-
239
- expect ( event . exception . values [ 0 ] . type ) . toEqual ( 'React ErrorBoundary Error' ) ;
240
- expect ( event . exception . values [ 0 ] . stacktrace . frames ) . toEqual ( [
241
- {
242
- colno : expect . any ( Number ) ,
243
- filename : expect . stringContaining ( 'errorboundary.test.tsx' ) ,
244
- function : 'TestApp' ,
245
- in_app : true ,
246
- lineno : expect . any ( Number ) ,
247
- } ,
248
- {
249
- colno : expect . any ( Number ) ,
250
- filename : expect . stringContaining ( 'errorboundary.tsx' ) ,
251
- function : 'ErrorBoundary' ,
252
- in_app : true ,
253
- lineno : expect . any ( Number ) ,
254
- } ,
255
- {
256
- colno : expect . any ( Number ) ,
257
- filename : expect . stringContaining ( 'errorboundary.test.tsx' ) ,
258
- function : 'Bam' ,
259
- in_app : true ,
260
- lineno : expect . any ( Number ) ,
261
- } ,
262
- {
263
- colno : expect . any ( Number ) ,
264
- filename : expect . stringContaining ( 'errorboundary.test.tsx' ) ,
265
- function : 'Boo' ,
266
- in_app : true ,
267
- lineno : expect . any ( Number ) ,
268
- } ,
269
- ] ) ;
231
+ expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 1 ) ;
232
+ expect ( mockCaptureException ) . toHaveBeenLastCalledWith ( expect . any ( Error ) , {
233
+ contexts : { react : { componentStack : expect . any ( String ) } } ,
234
+ } ) ;
235
+
236
+ expect ( mockOnError . mock . calls [ 0 ] [ 0 ] ) . toEqual ( mockCaptureException . mock . calls [ 0 ] [ 0 ] ) ;
237
+
238
+ // Check if error.cause -> react component stack
239
+ const error = mockCaptureException . mock . calls [ 0 ] [ 0 ] ;
240
+ const cause = error . cause ;
241
+ expect ( cause . stack ) . toEqual ( mockCaptureException . mock . calls [ 0 ] [ 1 ] . contexts . react . componentStack ) ;
242
+ expect ( cause . name ) . toContain ( 'React ErrorBoundary' ) ;
243
+ expect ( cause . message ) . toEqual ( error . message ) ;
270
244
} ) ;
271
245
272
246
it ( 'calls `beforeCapture()` when an error occurs' , ( ) => {
273
247
const mockBeforeCapture = jest . fn ( ) ;
274
248
275
249
const testBeforeCapture = ( ...args : any [ ] ) => {
276
- expect ( mockCaptureEvent ) . toHaveBeenCalledTimes ( 0 ) ;
250
+ expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 0 ) ;
277
251
mockBeforeCapture ( ...args ) ;
278
252
} ;
279
253
@@ -284,14 +258,14 @@ describe('ErrorBoundary', () => {
284
258
) ;
285
259
286
260
expect ( mockBeforeCapture ) . toHaveBeenCalledTimes ( 0 ) ;
287
- expect ( mockCaptureEvent ) . toHaveBeenCalledTimes ( 0 ) ;
261
+ expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 0 ) ;
288
262
289
263
const btn = screen . getByTestId ( 'errorBtn' ) ;
290
264
fireEvent . click ( btn ) ;
291
265
292
266
expect ( mockBeforeCapture ) . toHaveBeenCalledTimes ( 1 ) ;
293
267
expect ( mockBeforeCapture ) . toHaveBeenLastCalledWith ( expect . any ( Scope ) , expect . any ( Error ) , expect . any ( String ) ) ;
294
- expect ( mockCaptureEvent ) . toHaveBeenCalledTimes ( 1 ) ;
268
+ expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 1 ) ;
295
269
} ) ;
296
270
297
271
it ( 'shows a Sentry Report Dialog with correct options' , ( ) => {
0 commit comments