@@ -20,12 +20,10 @@ describe('ReactIncrementalErrorLogging', () => {
2020 ReactNoop = require ( 'react-noop-renderer' ) ;
2121 } ) ;
2222
23- function normalizeCodeLocInfo ( str ) {
24- return str && str . replace ( / \( a t .+ ?: \d + \) / g, '(at **)' ) ;
25- }
26-
2723 it ( 'should log errors that occur during the begin phase' , ( ) => {
28- spyOnDevAndProd ( console , 'error' ) ;
24+ // Errors are redundantly logged in production mode by ReactFiberErrorLogger.
25+ // It's okay to ignore them for the purpose of this test.
26+ spyOnProd ( console , 'error' ) ;
2927
3028 class ErrorThrowingComponent extends React . Component {
3129 componentWillMount ( ) {
@@ -41,36 +39,29 @@ describe('ReactIncrementalErrorLogging', () => {
4139 }
4240 }
4341
44- try {
45- ReactNoop . render (
46- < div >
47- < span >
48- < ErrorThrowingComponent />
49- </ span >
50- </ div > ,
51- ) ;
52- ReactNoop . flushDeferredPri ( ) ;
53- } catch ( error ) { }
42+ ReactNoop . render (
43+ < div >
44+ < span >
45+ < ErrorThrowingComponent />
46+ </ span >
47+ </ div > ,
48+ ) ;
5449
55- expect ( console . error . calls . count ( ) ) . toBe ( 1 ) ;
56- const errorMessage = console . error . calls . argsFor ( 0 ) [ 0 ] ;
57- if ( __DEV__ ) {
58- expect ( normalizeCodeLocInfo ( errorMessage ) ) . toContain (
50+ expect ( ( ) => {
51+ expect ( ReactNoop . flushDeferredPri ) . toWarnDev (
5952 'The above error occurred in the <ErrorThrowingComponent> component:\n' +
6053 ' in ErrorThrowingComponent (at **)\n' +
6154 ' in span (at **)\n' +
62- ' in div (at **)' ,
55+ ' in div (at **)\n\n' +
56+ 'Consider adding an error boundary to your tree to customize error handling behavior.' ,
6357 ) ;
64- expect ( errorMessage ) . toContain (
65- 'Consider adding an error boundary to your tree to customize error handling behavior.' ,
66- ) ;
67- } else {
68- expect ( errorMessage . message ) . toContain ( 'componentWillMount error' ) ;
69- }
58+ } ) . toThrowError ( 'componentWillMount error' ) ;
7059 } ) ;
7160
7261 it ( 'should log errors that occur during the commit phase' , ( ) => {
73- spyOnDevAndProd ( console , 'error' ) ;
62+ // Errors are redundantly logged in production mode by ReactFiberErrorLogger.
63+ // It's okay to ignore them for the purpose of this test.
64+ spyOnProd ( console , 'error' ) ;
7465
7566 class ErrorThrowingComponent extends React . Component {
7667 componentDidMount ( ) {
@@ -86,32 +77,23 @@ describe('ReactIncrementalErrorLogging', () => {
8677 }
8778 }
8879
89- try {
90- ReactNoop . render (
91- < div >
92- < span >
93- < ErrorThrowingComponent />
94- </ span >
95- </ div > ,
96- ) ;
97- ReactNoop . flushDeferredPri ( ) ;
98- } catch ( error ) { }
80+ ReactNoop . render (
81+ < div >
82+ < span >
83+ < ErrorThrowingComponent />
84+ </ span >
85+ </ div > ,
86+ ) ;
9987
100- expect ( console . error . calls . count ( ) ) . toBe ( 1 ) ;
101- const errorMessage = console . error . calls . argsFor ( 0 ) [ 0 ] ;
102- if ( __DEV__ ) {
103- expect ( normalizeCodeLocInfo ( errorMessage ) ) . toContain (
88+ expect ( ( ) => {
89+ expect ( ReactNoop . flushDeferredPri ) . toWarnDev (
10490 'The above error occurred in the <ErrorThrowingComponent> component:\n' +
10591 ' in ErrorThrowingComponent (at **)\n' +
10692 ' in span (at **)\n' +
107- ' in div (at **)' ,
108- ) ;
109- expect ( errorMessage ) . toContain (
110- 'Consider adding an error boundary to your tree to customize error handling behavior.' ,
93+ ' in div (at **)\n\n' +
94+ 'Consider adding an error boundary to your tree to customize error handling behavior.' ,
11195 ) ;
112- } else {
113- expect ( errorMessage . message ) . toBe ( 'componentDidMount error' ) ;
114- }
96+ } ) . toThrowError ( 'componentDidMount error' ) ;
11597 } ) ;
11698
11799 it ( 'should ignore errors thrown in log method to prevent cycle' , ( ) => {
@@ -120,6 +102,8 @@ describe('ReactIncrementalErrorLogging', () => {
120102 try {
121103 React = require ( 'react' ) ;
122104 ReactNoop = require ( 'react-noop-renderer' ) ;
105+
106+ // TODO Update this test to use toWarnDev() matcher if possible
123107 spyOnDevAndProd ( console , 'error' ) ;
124108
125109 class ErrorThrowingComponent extends React . Component {
@@ -167,7 +151,9 @@ describe('ReactIncrementalErrorLogging', () => {
167151 } ) ;
168152
169153 it ( 'should relay info about error boundary and retry attempts if applicable' , ( ) => {
170- spyOnDevAndProd ( console , 'error' ) ;
154+ // Errors are redundantly logged in production mode by ReactFiberErrorLogger.
155+ // It's okay to ignore them for the purpose of this test.
156+ spyOnProd ( console , 'error' ) ;
171157
172158 class ParentComponent extends React . Component {
173159 render ( ) {
@@ -203,30 +189,27 @@ describe('ReactIncrementalErrorLogging', () => {
203189 }
204190 }
205191
206- try {
207- ReactNoop . render ( < ParentComponent /> ) ;
208- ReactNoop . flush ( ) ;
209- } catch ( error ) { }
192+ ReactNoop . render ( < ParentComponent /> ) ;
210193
211- expect ( renderAttempts ) . toBe ( 2 ) ;
212- expect ( handleErrorCalls . length ) . toBe ( 1 ) ;
213- expect ( console . error . calls . count ( ) ) . toBe ( 2 ) ;
214- if ( __DEV__ ) {
215- expect ( console . error . calls . argsFor ( 0 ) [ 0 ] ) . toContain (
216- 'The above error occurred in the <ErrorThrowingComponent> component:' ,
217- ) ;
218- expect ( console . error . calls . argsFor ( 0 ) [ 0 ] ) . toContain (
219- 'React will try to recreate this component tree from scratch ' +
194+ expect ( ( ) => {
195+ expect ( ReactNoop . flush ) . toWarnDev ( [
196+ 'The above error occurred in the <ErrorThrowingComponent> component:\n' +
197+ ' in ErrorThrowingComponent (at **)\n' +
198+ ' in ErrorBoundaryComponent (at **)\n' +
199+ ' in ParentComponent (at **)\n\n' +
200+ 'React will try to recreate this component tree from scratch ' +
220201 'using the error boundary you provided, ErrorBoundaryComponent.' ,
221- ) ;
222- expect ( console . error . calls . argsFor ( 1 ) [ 0 ] ) . toContain (
223- 'The above error occurred in the <ErrorThrowingComponent> component:' ,
224- ) ;
225- expect ( console . error . calls . argsFor ( 1 ) [ 0 ] ) . toContain (
226- 'This error was initially handled by the error boundary ErrorBoundaryComponent.\n' +
202+ 'The above error occurred in the <ErrorThrowingComponent> component:\n' +
203+ ' in ErrorThrowingComponent (at **)\n' +
204+ ' in ErrorBoundaryComponent (at **)\n' +
205+ ' in ParentComponent (at **)\n\n' +
206+ 'This error was initially handled by the error boundary ErrorBoundaryComponent.\n' +
227207 'Recreating the tree from scratch failed so React will unmount the tree.' ,
228- ) ;
229- }
208+ ] ) ;
209+ } ) . toThrowError ( 'componentDidMount error' ) ;
210+
211+ expect ( renderAttempts ) . toBe ( 2 ) ;
212+ expect ( handleErrorCalls . length ) . toBe ( 1 ) ;
230213 } ) ;
231214} ) ;
232215
0 commit comments