1111
1212'use strict' ;
1313
14+ var ReactDOMFeatureFlags = require ( 'ReactDOMFeatureFlags' ) ;
15+
1416var React ;
1517var ReactDOM ;
1618
@@ -33,6 +35,9 @@ describe('ReactErrorBoundaries', () => {
3335 var Normal ;
3436
3537 beforeEach ( ( ) => {
38+ // TODO: Fiber isn't error resilient and one test can bring down them all.
39+ jest . resetModuleRegistry ( ) ;
40+
3641 ReactDOM = require ( 'ReactDOM' ) ;
3742 React = require ( 'React' ) ;
3843
@@ -459,52 +464,93 @@ describe('ReactErrorBoundaries', () => {
459464 } ;
460465 } ) ;
461466
462- // Known limitation: error boundary only "sees" errors caused by updates
463- // flowing through it. This might be easier to fix in Fiber.
464- it ( 'currently does not catch errors originating downstream' , ( ) => {
465- var fail = false ;
466- class Stateful extends React . Component {
467- state = { shouldThrow : false } ;
467+ if ( ReactDOMFeatureFlags . useFiber ) {
468+ // This test implements a new feature in Fiber.
469+ it ( 'catches errors originating downstream' , ( ) => {
470+ var fail = false ;
471+ class Stateful extends React . Component {
472+ state = { shouldThrow : false } ;
473+
474+ render ( ) {
475+ if ( fail ) {
476+ log . push ( 'Stateful render [!]' ) ;
477+ throw new Error ( 'Hello' ) ;
478+ }
479+ return < div /> ;
480+ }
481+ }
468482
469- render ( ) {
470- if ( fail ) {
471- log . push ( 'Stateful render [!]' ) ;
472- throw new Error ( 'Hello' ) ;
483+ var statefulInst ;
484+ var container = document . createElement ( 'div' ) ;
485+ ReactDOM . render (
486+ < ErrorBoundary >
487+ < Stateful ref = { inst => statefulInst = inst } />
488+ </ ErrorBoundary > ,
489+ container
490+ ) ;
491+
492+ log . length = 0 ;
493+ expect ( ( ) => {
494+ fail = true ;
495+ statefulInst . forceUpdate ( ) ;
496+ } ) . not . toThrow ( ) ;
497+
498+ expect ( log ) . toEqual ( [
499+ 'Stateful render [!]' ,
500+ 'ErrorBoundary unstable_handleError' ,
501+ 'ErrorBoundary render error' ,
502+ 'ErrorBoundary componentDidUpdate' ,
503+ ] ) ;
504+
505+ log . length = 0 ;
506+ ReactDOM . unmountComponentAtNode ( container ) ;
507+ expect ( log ) . toEqual ( [
508+ 'ErrorBoundary componentWillUnmount' ,
509+ ] ) ;
510+ } ) ;
511+ } else {
512+ // Known limitation: error boundary only "sees" errors caused by updates
513+ // flowing through it. This is fixed in Fiber.
514+ it ( 'currently does not catch errors originating downstream' , ( ) => {
515+ var fail = false ;
516+ class Stateful extends React . Component {
517+ state = { shouldThrow : false } ;
518+
519+ render ( ) {
520+ if ( fail ) {
521+ log . push ( 'Stateful render [!]' ) ;
522+ throw new Error ( 'Hello' ) ;
523+ }
524+ return < div /> ;
473525 }
474- return < div /> ;
475526 }
476- }
477527
478- var statefulInst ;
479- var container = document . createElement ( 'div' ) ;
480- ReactDOM . render (
481- < ErrorBoundary >
482- < Stateful ref = { inst => statefulInst = inst } />
483- </ ErrorBoundary > ,
484- container
485- ) ;
528+ var statefulInst ;
529+ var container = document . createElement ( 'div' ) ;
530+ ReactDOM . render (
531+ < ErrorBoundary >
532+ < Stateful ref = { inst => statefulInst = inst } />
533+ </ ErrorBoundary > ,
534+ container
535+ ) ;
486536
487- log . length = 0 ;
488- expect ( ( ) => {
489- fail = true ;
490- statefulInst . forceUpdate ( ) ;
491- } ) . toThrow ( ) ;
537+ log . length = 0 ;
538+ expect ( ( ) => {
539+ fail = true ;
540+ statefulInst . forceUpdate ( ) ;
541+ } ) . toThrow ( ) ;
492542
493- expect ( log ) . toEqual ( [
494- 'Stateful render [!]' ,
495- // FIXME: uncomment when downstream errors get caught.
496- // Catch and render an error message
497- // 'ErrorBoundary unstable_handleError',
498- // 'ErrorBoundary render error',
499- // 'ErrorBoundary componentDidUpdate',
500- ] ) ;
543+ expect ( log ) . toEqual ( [
544+ 'Stateful render [!]' ,
545+ ] ) ;
501546
502- log . length = 0 ;
503- ReactDOM . unmountComponentAtNode ( container ) ;
504- expect ( log ) . toEqual ( [
505- 'ErrorBoundary componentWillUnmount' ,
506- ] ) ;
507- } ) ;
547+ log . length = 0 ;
548+ ReactDOM . unmountComponentAtNode ( container ) ;
549+ expect ( log ) . toEqual ( [
550+ 'ErrorBoundary componentWillUnmount' ,
551+ ] ) ;
552+ } ) ;
553+ }
508554
509555 it ( 'renders an error state if child throws in render' , ( ) => {
510556 var container = document . createElement ( 'div' ) ;
@@ -860,7 +906,7 @@ describe('ReactErrorBoundaries', () => {
860906 'BrokenRender render [!]' ,
861907 // Handle error:
862908 'ErrorBoundary unstable_handleError' ,
863- // Child ref wasn't ( and won 't be) set but there's no harm in clearing :
909+ // TODO: This is unnecessary, and Fiber doesn 't do it :
864910 'Child ref is set to null' ,
865911 'ErrorBoundary render error' ,
866912 // Ref to error message should get set:
0 commit comments