@@ -6,16 +6,16 @@ import { useState } from 'react';
66
77import { ErrorBoundary , ErrorBoundaryProps , UNKNOWN_COMPONENT , withErrorBoundary } from '../src/errorboundary' ;
88
9- const mockCaptureEvent = jest . fn ( ) ;
9+ const mockCaptureException = jest . fn ( ) ;
1010const mockShowReportDialog = jest . fn ( ) ;
1111const EVENT_ID = 'test-id-123' ;
1212
1313jest . mock ( '@sentry/browser' , ( ) => {
1414 const actual = jest . requireActual ( '@sentry/browser' ) ;
1515 return {
1616 ...actual ,
17- captureEvent : ( event : Event ) => {
18- mockCaptureEvent ( event ) ;
17+ captureException : ( ... args : unknown [ ] ) => {
18+ mockCaptureException ( ... args ) ;
1919 return EVENT_ID ;
2020 } ,
2121 showReportDialog : ( options : any ) => {
@@ -74,7 +74,7 @@ describe('ErrorBoundary', () => {
7474 jest . spyOn ( console , 'error' ) . mockImplementation ( ) ;
7575
7676 afterEach ( ( ) => {
77- mockCaptureEvent . mockClear ( ) ;
77+ mockCaptureException . mockClear ( ) ;
7878 mockShowReportDialog . mockClear ( ) ;
7979 } ) ;
8080
@@ -220,60 +220,34 @@ describe('ErrorBoundary', () => {
220220 ) ;
221221
222222 expect ( mockOnError ) . toHaveBeenCalledTimes ( 0 ) ;
223- expect ( mockCaptureEvent ) . toHaveBeenCalledTimes ( 0 ) ;
223+ expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 0 ) ;
224224
225225 const btn = screen . getByTestId ( 'errorBtn' ) ;
226226 fireEvent . click ( btn ) ;
227227
228228 expect ( mockOnError ) . toHaveBeenCalledTimes ( 1 ) ;
229229 expect ( mockOnError ) . toHaveBeenCalledWith ( expect . any ( Error ) , expect . any ( String ) , expect . any ( String ) ) ;
230230
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 ) ;
270244 } ) ;
271245
272246 it ( 'calls `beforeCapture()` when an error occurs' , ( ) => {
273247 const mockBeforeCapture = jest . fn ( ) ;
274248
275249 const testBeforeCapture = ( ...args : any [ ] ) => {
276- expect ( mockCaptureEvent ) . toHaveBeenCalledTimes ( 0 ) ;
250+ expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 0 ) ;
277251 mockBeforeCapture ( ...args ) ;
278252 } ;
279253
@@ -284,14 +258,14 @@ describe('ErrorBoundary', () => {
284258 ) ;
285259
286260 expect ( mockBeforeCapture ) . toHaveBeenCalledTimes ( 0 ) ;
287- expect ( mockCaptureEvent ) . toHaveBeenCalledTimes ( 0 ) ;
261+ expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 0 ) ;
288262
289263 const btn = screen . getByTestId ( 'errorBtn' ) ;
290264 fireEvent . click ( btn ) ;
291265
292266 expect ( mockBeforeCapture ) . toHaveBeenCalledTimes ( 1 ) ;
293267 expect ( mockBeforeCapture ) . toHaveBeenLastCalledWith ( expect . any ( Scope ) , expect . any ( Error ) , expect . any ( String ) ) ;
294- expect ( mockCaptureEvent ) . toHaveBeenCalledTimes ( 1 ) ;
268+ expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 1 ) ;
295269 } ) ;
296270
297271 it ( 'shows a Sentry Report Dialog with correct options' , ( ) => {
0 commit comments