@@ -12,6 +12,8 @@ import { useState } from 'react';
1212import type { ErrorBoundaryProps , FallbackRender } from '../src/errorboundary' ;
1313import { ErrorBoundary , UNKNOWN_COMPONENT , withErrorBoundary } from '../src/errorboundary' ;
1414
15+ const mockScope = new Scope ( ) ;
16+ const scopeSetContextSpy = vi . spyOn ( mockScope , 'setContext' ) ;
1517const mockCaptureException = vi . fn ( ) ;
1618const mockShowReportDialog = vi . fn ( ) ;
1719const mockClientOn = vi . fn ( ) ;
@@ -27,6 +29,9 @@ vi.mock('@sentry/browser', async requireActual => {
2729 showReportDialog : ( options : any ) => {
2830 mockShowReportDialog ( options ) ;
2931 } ,
32+ withScope : ( callback : ( scope : any ) => any ) => {
33+ return callback ( mockScope ) ;
34+ } ,
3035 } ;
3136} ) ;
3237
@@ -102,6 +107,7 @@ describe('ErrorBoundary', () => {
102107 mockCaptureException . mockClear ( ) ;
103108 mockShowReportDialog . mockClear ( ) ;
104109 mockClientOn . mockClear ( ) ;
110+ ( mockScope . setContext as any ) . mockClear ( ) ;
105111 } ) ;
106112
107113 it ( 'renders null if not given a valid `fallback` prop' , ( ) => {
@@ -265,20 +271,19 @@ describe('ErrorBoundary', () => {
265271
266272 expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 1 ) ;
267273 expect ( mockCaptureException ) . toHaveBeenLastCalledWith ( expect . any ( Error ) , {
268- captureContext : {
269- contexts : { react : { componentStack : expect . any ( String ) } } ,
270- } ,
271274 mechanism : { handled : true } ,
272275 } ) ;
273276
277+ expect ( scopeSetContextSpy ) . toHaveBeenCalledTimes ( 1 ) ;
278+ expect ( scopeSetContextSpy ) . toHaveBeenCalledWith ( 'react' , { componentStack : expect . any ( String ) } ) ;
279+
274280 expect ( mockOnError . mock . calls [ 0 ] ?. [ 0 ] ) . toEqual ( mockCaptureException . mock . calls [ 0 ] ?. [ 0 ] ) ;
275281
276282 // Check if error.cause -> react component stack
277283 const error = mockCaptureException . mock . calls [ 0 ] ?. [ 0 ] ;
278284 const cause = error . cause ;
279- expect ( cause . stack ) . toEqual (
280- mockCaptureException . mock . calls [ 0 ] ?. [ 1 ] ?. captureContext . contexts . react . componentStack ,
281- ) ;
285+
286+ expect ( cause . stack ) . toEqual ( scopeSetContextSpy . mock . calls [ 0 ] ?. [ 1 ] ?. componentStack ) ;
282287 expect ( cause . name ) . toContain ( 'React ErrorBoundary' ) ;
283288 expect ( cause . message ) . toEqual ( error . message ) ;
284289 } ) ;
@@ -325,12 +330,12 @@ describe('ErrorBoundary', () => {
325330
326331 expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 1 ) ;
327332 expect ( mockCaptureException ) . toHaveBeenLastCalledWith ( 'bam' , {
328- captureContext : {
329- contexts : { react : { componentStack : expect . any ( String ) } } ,
330- } ,
331333 mechanism : { handled : true } ,
332334 } ) ;
333335
336+ expect ( scopeSetContextSpy ) . toHaveBeenCalledTimes ( 1 ) ;
337+ expect ( scopeSetContextSpy ) . toHaveBeenCalledWith ( 'react' , { componentStack : expect . any ( String ) } ) ;
338+
334339 // Check if error.cause -> react component stack
335340 const error = mockCaptureException . mock . calls [ 0 ] ?. [ 0 ] ;
336341 expect ( error . cause ) . not . toBeDefined ( ) ;
@@ -364,21 +369,19 @@ describe('ErrorBoundary', () => {
364369
365370 expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 1 ) ;
366371 expect ( mockCaptureException ) . toHaveBeenLastCalledWith ( expect . any ( Error ) , {
367- captureContext : {
368- contexts : { react : { componentStack : expect . any ( String ) } } ,
369- } ,
370372 mechanism : { handled : true } ,
371373 } ) ;
372374
375+ expect ( scopeSetContextSpy ) . toHaveBeenCalledTimes ( 1 ) ;
376+ expect ( scopeSetContextSpy ) . toHaveBeenCalledWith ( 'react' , { componentStack : expect . any ( String ) } ) ;
377+
373378 expect ( mockOnError . mock . calls [ 0 ] ?. [ 0 ] ) . toEqual ( mockCaptureException . mock . calls [ 0 ] ?. [ 0 ] ) ;
374379
375380 const thirdError = mockCaptureException . mock . calls [ 0 ] ?. [ 0 ] ;
376381 const secondError = thirdError . cause ;
377382 const firstError = secondError . cause ;
378383 const cause = firstError . cause ;
379- expect ( cause . stack ) . toEqual (
380- mockCaptureException . mock . calls [ 0 ] ?. [ 1 ] ?. captureContext . contexts . react . componentStack ,
381- ) ;
384+ expect ( cause . stack ) . toEqual ( scopeSetContextSpy . mock . calls [ 0 ] ?. [ 1 ] ?. componentStack ) ;
382385 expect ( cause . name ) . toContain ( 'React ErrorBoundary' ) ;
383386 expect ( cause . message ) . toEqual ( thirdError . message ) ;
384387 } ) ;
@@ -410,20 +413,18 @@ describe('ErrorBoundary', () => {
410413
411414 expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 1 ) ;
412415 expect ( mockCaptureException ) . toHaveBeenLastCalledWith ( expect . any ( Error ) , {
413- captureContext : {
414- contexts : { react : { componentStack : expect . any ( String ) } } ,
415- } ,
416416 mechanism : { handled : true } ,
417417 } ) ;
418418
419+ expect ( scopeSetContextSpy ) . toHaveBeenCalledTimes ( 1 ) ;
420+ expect ( scopeSetContextSpy ) . toHaveBeenCalledWith ( 'react' , { componentStack : expect . any ( String ) } ) ;
421+
419422 expect ( mockOnError . mock . calls [ 0 ] ?. [ 0 ] ) . toEqual ( mockCaptureException . mock . calls [ 0 ] ?. [ 0 ] ) ;
420423
421424 const error = mockCaptureException . mock . calls [ 0 ] ?. [ 0 ] ;
422425 const cause = error . cause ;
423426 // We need to make sure that recursive error.cause does not cause infinite loop
424- expect ( cause . stack ) . not . toEqual (
425- mockCaptureException . mock . calls [ 0 ] ?. [ 1 ] ?. captureContext . contexts . react . componentStack ,
426- ) ;
427+ expect ( cause . stack ) . not . toEqual ( scopeSetContextSpy . mock . calls [ 0 ] ?. [ 1 ] ?. componentStack ) ;
427428 expect ( cause . name ) . not . toContain ( 'React ErrorBoundary' ) ;
428429 } ) ;
429430
@@ -580,11 +581,11 @@ describe('ErrorBoundary', () => {
580581
581582 expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 1 ) ;
582583 expect ( mockCaptureException ) . toHaveBeenLastCalledWith ( expect . any ( Object ) , {
583- captureContext : {
584- contexts : { react : { componentStack : expect . any ( String ) } } ,
585- } ,
586584 mechanism : { handled : expected } ,
587585 } ) ;
586+
587+ expect ( scopeSetContextSpy ) . toHaveBeenCalledTimes ( 1 ) ;
588+ expect ( scopeSetContextSpy ) . toHaveBeenCalledWith ( 'react' , { componentStack : expect . any ( String ) } ) ;
588589 } ,
589590 ) ;
590591 } ) ;
0 commit comments