1+ /**
2+ * @vitest -environment jsdom
3+ */
4+ import { afterEach , describe , expect , it , vi } from 'vitest' ;
5+
16import { Scope , getClient , setCurrentClient } from '@sentry/browser' ;
27import type { Client } from '@sentry/core' ;
38import { fireEvent , render , screen } from '@testing-library/react' ;
@@ -7,15 +12,14 @@ import { useState } from 'react';
712import type { ErrorBoundaryProps , FallbackRender } from '../src/errorboundary' ;
813import { ErrorBoundary , UNKNOWN_COMPONENT , withErrorBoundary } from '../src/errorboundary' ;
914
10- const mockCaptureException = jest . fn ( ) ;
11- const mockShowReportDialog = jest . fn ( ) ;
12- const mockClientOn = jest . fn ( ) ;
15+ const mockCaptureException = vi . fn ( ) ;
16+ const mockShowReportDialog = vi . fn ( ) ;
17+ const mockClientOn = vi . fn ( ) ;
1318const EVENT_ID = 'test-id-123' ;
1419
15- jest . mock ( '@sentry/browser' , ( ) => {
16- const actual = jest . requireActual ( '@sentry/browser' ) ;
20+ vi . mock ( '@sentry/browser' , async requireActual => {
1721 return {
18- ...actual ,
22+ ...( await requireActual ( ) ) ,
1923 captureException : ( ...args : unknown [ ] ) => {
2024 mockCaptureException ( ...args ) ;
2125 return EVENT_ID ;
@@ -92,7 +96,7 @@ describe('withErrorBoundary', () => {
9296} ) ;
9397
9498describe ( 'ErrorBoundary' , ( ) => {
95- jest . spyOn ( console , 'error' ) . mockImplementation ( ) ;
99+ vi . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
96100
97101 afterEach ( ( ) => {
98102 mockCaptureException . mockClear ( ) ;
@@ -141,7 +145,7 @@ describe('ErrorBoundary', () => {
141145 } ) ;
142146
143147 it ( 'calls `onMount` when mounted' , ( ) => {
144- const mockOnMount = jest . fn ( ) ;
148+ const mockOnMount = vi . fn ( ) ;
145149 render (
146150 < ErrorBoundary fallback = { < h1 > Error Component</ h1 > } onMount = { mockOnMount } >
147151 < h1 > children</ h1 >
@@ -152,7 +156,7 @@ describe('ErrorBoundary', () => {
152156 } ) ;
153157
154158 it ( 'calls `onUnmount` when unmounted' , ( ) => {
155- const mockOnUnmount = jest . fn ( ) ;
159+ const mockOnUnmount = vi . fn ( ) ;
156160 const { unmount } = render (
157161 < ErrorBoundary fallback = { < h1 > Error Component</ h1 > } onUnmount = { mockOnUnmount } >
158162 < h1 > children</ h1 >
@@ -243,7 +247,7 @@ describe('ErrorBoundary', () => {
243247
244248 describe ( 'error' , ( ) => {
245249 it ( 'calls `componentDidCatch() when an error occurs`' , ( ) => {
246- const mockOnError = jest . fn ( ) ;
250+ const mockOnError = vi . fn ( ) ;
247251 render (
248252 < TestApp fallback = { < p > You have hit an error</ p > } onError = { mockOnError } >
249253 < h1 > children</ h1 >
@@ -267,12 +271,14 @@ describe('ErrorBoundary', () => {
267271 mechanism : { handled : true } ,
268272 } ) ;
269273
270- expect ( mockOnError . mock . calls [ 0 ] [ 0 ] ) . toEqual ( mockCaptureException . mock . calls [ 0 ] [ 0 ] ) ;
274+ expect ( mockOnError . mock . calls [ 0 ] ?. [ 0 ] ) . toEqual ( mockCaptureException . mock . calls [ 0 ] ?. [ 0 ] ) ;
271275
272276 // Check if error.cause -> react component stack
273- const error = mockCaptureException . mock . calls [ 0 ] [ 0 ] ;
277+ const error = mockCaptureException . mock . calls [ 0 ] ?. [ 0 ] ;
274278 const cause = error . cause ;
275- expect ( cause . stack ) . toEqual ( mockCaptureException . mock . calls [ 0 ] [ 1 ] ?. captureContext . contexts . react . componentStack ) ;
279+ expect ( cause . stack ) . toEqual (
280+ mockCaptureException . mock . calls [ 0 ] ?. [ 1 ] ?. captureContext . contexts . react . componentStack ,
281+ ) ;
276282 expect ( cause . name ) . toContain ( 'React ErrorBoundary' ) ;
277283 expect ( cause . message ) . toEqual ( error . message ) ;
278284 } ) ;
@@ -326,12 +332,12 @@ describe('ErrorBoundary', () => {
326332 } ) ;
327333
328334 // Check if error.cause -> react component stack
329- const error = mockCaptureException . mock . calls [ 0 ] [ 0 ] ;
335+ const error = mockCaptureException . mock . calls [ 0 ] ?. [ 0 ] ;
330336 expect ( error . cause ) . not . toBeDefined ( ) ;
331337 } ) ;
332338
333339 it ( 'handles when `error.cause` is nested' , ( ) => {
334- const mockOnError = jest . fn ( ) ;
340+ const mockOnError = vi . fn ( ) ;
335341
336342 function CustomBam ( ) : JSX . Element {
337343 const firstError = new Error ( 'bam' ) ;
@@ -364,19 +370,21 @@ describe('ErrorBoundary', () => {
364370 mechanism : { handled : true } ,
365371 } ) ;
366372
367- expect ( mockOnError . mock . calls [ 0 ] [ 0 ] ) . toEqual ( mockCaptureException . mock . calls [ 0 ] [ 0 ] ) ;
373+ expect ( mockOnError . mock . calls [ 0 ] ?. [ 0 ] ) . toEqual ( mockCaptureException . mock . calls [ 0 ] ?. [ 0 ] ) ;
368374
369- const thirdError = mockCaptureException . mock . calls [ 0 ] [ 0 ] ;
375+ const thirdError = mockCaptureException . mock . calls [ 0 ] ?. [ 0 ] ;
370376 const secondError = thirdError . cause ;
371377 const firstError = secondError . cause ;
372378 const cause = firstError . cause ;
373- expect ( cause . stack ) . toEqual ( mockCaptureException . mock . calls [ 0 ] [ 1 ] ?. captureContext . contexts . react . componentStack ) ;
379+ expect ( cause . stack ) . toEqual (
380+ mockCaptureException . mock . calls [ 0 ] ?. [ 1 ] ?. captureContext . contexts . react . componentStack ,
381+ ) ;
374382 expect ( cause . name ) . toContain ( 'React ErrorBoundary' ) ;
375383 expect ( cause . message ) . toEqual ( thirdError . message ) ;
376384 } ) ;
377385
378386 it ( 'handles when `error.cause` is recursive' , ( ) => {
379- const mockOnError = jest . fn ( ) ;
387+ const mockOnError = vi . fn ( ) ;
380388
381389 function CustomBam ( ) : JSX . Element {
382390 const firstError = new Error ( 'bam' ) ;
@@ -408,19 +416,19 @@ describe('ErrorBoundary', () => {
408416 mechanism : { handled : true } ,
409417 } ) ;
410418
411- expect ( mockOnError . mock . calls [ 0 ] [ 0 ] ) . toEqual ( mockCaptureException . mock . calls [ 0 ] [ 0 ] ) ;
419+ expect ( mockOnError . mock . calls [ 0 ] ?. [ 0 ] ) . toEqual ( mockCaptureException . mock . calls [ 0 ] ?. [ 0 ] ) ;
412420
413- const error = mockCaptureException . mock . calls [ 0 ] [ 0 ] ;
421+ const error = mockCaptureException . mock . calls [ 0 ] ?. [ 0 ] ;
414422 const cause = error . cause ;
415423 // We need to make sure that recursive error.cause does not cause infinite loop
416424 expect ( cause . stack ) . not . toEqual (
417- mockCaptureException . mock . calls [ 0 ] [ 1 ] ?. captureContext . contexts . react . componentStack ,
425+ mockCaptureException . mock . calls [ 0 ] ?. [ 1 ] ?. captureContext . contexts . react . componentStack ,
418426 ) ;
419427 expect ( cause . name ) . not . toContain ( 'React ErrorBoundary' ) ;
420428 } ) ;
421429
422430 it ( 'calls `beforeCapture()` when an error occurs' , ( ) => {
423- const mockBeforeCapture = jest . fn ( ) ;
431+ const mockBeforeCapture = vi . fn ( ) ;
424432
425433 const testBeforeCapture = ( ...args : any [ ] ) => {
426434 expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 0 ) ;
@@ -516,7 +524,7 @@ describe('ErrorBoundary', () => {
516524 } ) ;
517525
518526 it ( 'calls `onReset()` when reset' , ( ) => {
519- const mockOnReset = jest . fn ( ) ;
527+ const mockOnReset = vi . fn ( ) ;
520528 render (
521529 < TestApp
522530 onReset = { mockOnReset }
0 commit comments