@@ -77,11 +77,9 @@ test("standard use-case", () => {
77
77
const { unmount} = render ( < App /> )
78
78
79
79
userEvent . type ( screen . getByRole ( "textbox" , { name : / u s e r n a m e / i} ) , "fail" )
80
-
81
- const [ [ actualError ] , [ componentStack ] ] = consoleError . mock . calls
82
- expect ( firstLine ( actualError as string ) ) . toMatchInlineSnapshot (
83
- `"Error: Uncaught [Error: 💥 CABOOM 💥]"` ,
84
- )
80
+ const calls = consoleError . mock . calls [ 0 ]
81
+ //@ts -expect-error - it's a mock
82
+ expect ( calls [ 1 ] ) . toMatchInlineSnapshot ( "[Error: 💥 CABOOM 💥]" )
85
83
expect ( cleanStack ( componentStack ) ) . toMatchInlineSnapshot ( `
86
84
"Error: Uncaught [Error: 💥 CABOOM 💥]
87
85
at reportException
@@ -149,7 +147,7 @@ test("fallbackRender prop", () => {
149
147
}
150
148
151
149
const { unmount} = render ( < App /> )
152
- expect ( consoleError ) . toHaveBeenCalledTimes ( 3 )
150
+ expect ( consoleError ) . toHaveBeenCalledTimes ( 1 )
153
151
consoleError . mockClear ( )
154
152
155
153
// the render prop API allows a single action to reset the app state
@@ -168,42 +166,31 @@ test("simple fallback is supported", () => {
168
166
< span > child</ span >
169
167
</ ErrorBoundary > ,
170
168
)
171
- expect ( consoleError ) . toHaveBeenCalledTimes ( 3 )
169
+ expect ( consoleError ) . toHaveBeenCalledTimes ( 1 )
172
170
consoleError . mockClear ( )
173
171
expect ( screen . getByText ( / o h n o / i) ) . to . exist
174
172
expect ( screen . queryByText ( / c h i l d / i) ) . to . not . exist
175
173
unmount ( )
176
174
} )
177
175
178
- test ( "withErrorBoundary HOC" , ( ) => {
176
+ test . only ( "withErrorBoundary HOC" , ( ) => {
179
177
const consoleError = console . error as MockedFunction < ( args : unknown [ ] ) => void >
180
178
181
179
const onErrorHandler = vi . fn ( )
182
180
const Boundary = withErrorBoundary (
183
181
( ) => {
184
182
throw new Error ( "💥 CABOOM 💥" )
185
183
} ,
186
- { FallbackComponent : ErrorFallback , onError : onErrorHandler } ,
184
+ {
185
+ FallbackComponent : ErrorFallback ,
186
+ onError : onErrorHandler ,
187
+ } ,
187
188
)
188
189
const { unmount} = render ( < Boundary /> )
189
-
190
- const [ [ actualError ] , [ componentStack ] ] = consoleError . mock . calls
191
- const firstLineOfError = firstLine ( actualError as string )
192
- expect ( firstLineOfError ) . toMatchInlineSnapshot ( `"Error: Uncaught [Error: 💥 CABOOM 💥]"` )
193
- expect ( cleanStack ( componentStack ) ) . toMatchInlineSnapshot ( `
194
- "Error: Uncaught [Error: 💥 CABOOM 💥]
195
- at reportException
196
- at innerInvokeEventListeners
197
- at invokeEventListeners
198
- at HTMLUnknownElementImpl._dispatch
199
- at HTMLUnknownElementImpl.dispatchEvent
200
- at HTMLUnknownElement.dispatchEvent
201
- at Object.invokeGuardedCallbackDev
202
- at invokeGuardedCallback
203
- at beginWork\$1
204
- at performUnitOfWork "
205
- ` )
206
- expect ( consoleError ) . toHaveBeenCalledTimes ( 3 )
190
+ const calls = consoleError . mock . calls [ 0 ]
191
+ //@ts -expect-error - it's a mock
192
+ expect ( calls [ 1 ] ) . toMatchInlineSnapshot ( "[Error: 💥 CABOOM 💥]" )
193
+ expect ( consoleError ) . toHaveBeenCalledTimes ( 1 )
207
194
consoleError . mockClear ( )
208
195
209
196
const [ error , onErrorComponentStack ] = ( onErrorHandler . mock . calls as [ [ Error , string ] ] ) [ 0 ]
@@ -265,7 +252,6 @@ test("requires either a fallback, fallbackRender, or FallbackComponent", () => {
265
252
let unmount : undefined | ( ( ) => void )
266
253
expect ( ( ) => {
267
254
const result = render (
268
- // @ts -expect-error we're testing the runtime check of missing props here
269
255
< ErrorBoundary >
270
256
< Bomb />
271
257
</ ErrorBoundary > ,
@@ -318,7 +304,7 @@ test("supports automatic reset of error boundary when resetKeys change", () => {
318
304
// blow it up
319
305
userEvent . click ( screen . getByText ( "toggle explode" ) )
320
306
expect ( screen . getByRole ( "alert" ) ) . to . exist
321
- expect ( consoleError ) . toHaveBeenCalledTimes ( 3 )
307
+ expect ( consoleError ) . toHaveBeenCalledTimes ( 1 )
322
308
consoleError . mockClear ( )
323
309
324
310
// recover via try again button
@@ -333,7 +319,7 @@ test("supports automatic reset of error boundary when resetKeys change", () => {
333
319
// blow it up again
334
320
userEvent . click ( screen . getByText ( "toggle explode" ) )
335
321
expect ( screen . getByRole ( "alert" ) ) . to . exist
336
- expect ( consoleError ) . toHaveBeenCalledTimes ( 3 )
322
+ expect ( consoleError ) . toHaveBeenCalledTimes ( 1 )
337
323
consoleError . mockClear ( )
338
324
339
325
// recover via resetKeys change
@@ -348,7 +334,7 @@ test("supports automatic reset of error boundary when resetKeys change", () => {
348
334
// blow it up again
349
335
userEvent . click ( screen . getByText ( "toggle explode" ) )
350
336
expect ( screen . getByRole ( "alert" ) ) . to . exist
351
- expect ( consoleError ) . toHaveBeenCalledTimes ( 3 )
337
+ expect ( consoleError ) . toHaveBeenCalledTimes ( 1 )
352
338
consoleError . mockClear ( )
353
339
354
340
// toggles adding an extra resetKey to the array
@@ -358,7 +344,7 @@ test("supports automatic reset of error boundary when resetKeys change", () => {
358
344
expect ( handleResetKeysChange ) . toHaveBeenCalledWith ( [ true ] , [ true , true ] )
359
345
handleResetKeysChange . mockClear ( )
360
346
expect ( screen . getByRole ( "alert" ) ) . to . exist
361
- expect ( consoleError ) . toHaveBeenCalledTimes ( 3 )
347
+ expect ( consoleError ) . toHaveBeenCalledTimes ( 1 )
362
348
consoleError . mockClear ( )
363
349
364
350
// toggle explode back to false
@@ -369,7 +355,7 @@ test("supports automatic reset of error boundary when resetKeys change", () => {
369
355
expect ( handleResetKeysChange ) . toHaveBeenCalledWith ( [ true , true ] , [ false , true ] )
370
356
expect ( screen . getByRole ( "alert" ) ) . to . exist
371
357
handleResetKeysChange . mockClear ( )
372
- expect ( consoleError ) . toHaveBeenCalledTimes ( 3 )
358
+ expect ( consoleError ) . toHaveBeenCalledTimes ( 1 )
373
359
consoleError . mockClear ( )
374
360
375
361
// toggle extra resetKey
@@ -411,7 +397,7 @@ test("supports reset via resetKeys right after error is triggered on component m
411
397
412
398
// it blows up on render
413
399
expect ( screen . queryByRole ( "alert" , { } ) ) . to . exist
414
- expect ( consoleError ) . toHaveBeenCalledTimes ( 3 )
400
+ expect ( consoleError ) . toHaveBeenCalledTimes ( 1 )
415
401
consoleError . mockClear ( )
416
402
417
403
// recover via "toggle explode" button
0 commit comments