@@ -20,12 +20,10 @@ describe('ReactIncrementalErrorLogging', () => {
20
20
ReactNoop = require ( 'react-noop-renderer' ) ;
21
21
} ) ;
22
22
23
- function normalizeCodeLocInfo ( str ) {
24
- return str && str . replace ( / \( a t .+ ?: \d + \) / g, '(at **)' ) ;
25
- }
26
-
27
23
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' ) ;
29
27
30
28
class ErrorThrowingComponent extends React . Component {
31
29
componentWillMount ( ) {
@@ -41,36 +39,29 @@ describe('ReactIncrementalErrorLogging', () => {
41
39
}
42
40
}
43
41
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
+ ) ;
54
49
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 (
59
52
'The above error occurred in the <ErrorThrowingComponent> component:\n' +
60
53
' in ErrorThrowingComponent (at **)\n' +
61
54
' 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.' ,
63
57
) ;
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' ) ;
70
59
} ) ;
71
60
72
61
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' ) ;
74
65
75
66
class ErrorThrowingComponent extends React . Component {
76
67
componentDidMount ( ) {
@@ -86,32 +77,23 @@ describe('ReactIncrementalErrorLogging', () => {
86
77
}
87
78
}
88
79
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
+ ) ;
99
87
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 (
104
90
'The above error occurred in the <ErrorThrowingComponent> component:\n' +
105
91
' in ErrorThrowingComponent (at **)\n' +
106
92
' 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.' ,
111
95
) ;
112
- } else {
113
- expect ( errorMessage . message ) . toBe ( 'componentDidMount error' ) ;
114
- }
96
+ } ) . toThrowError ( 'componentDidMount error' ) ;
115
97
} ) ;
116
98
117
99
it ( 'should ignore errors thrown in log method to prevent cycle' , ( ) => {
@@ -120,6 +102,8 @@ describe('ReactIncrementalErrorLogging', () => {
120
102
try {
121
103
React = require ( 'react' ) ;
122
104
ReactNoop = require ( 'react-noop-renderer' ) ;
105
+
106
+ // TODO Update this test to use toWarnDev() matcher if possible
123
107
spyOnDevAndProd ( console , 'error' ) ;
124
108
125
109
class ErrorThrowingComponent extends React . Component {
@@ -167,7 +151,9 @@ describe('ReactIncrementalErrorLogging', () => {
167
151
} ) ;
168
152
169
153
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' ) ;
171
157
172
158
class ParentComponent extends React . Component {
173
159
render ( ) {
@@ -203,30 +189,27 @@ describe('ReactIncrementalErrorLogging', () => {
203
189
}
204
190
}
205
191
206
- try {
207
- ReactNoop . render ( < ParentComponent /> ) ;
208
- ReactNoop . flush ( ) ;
209
- } catch ( error ) { }
192
+ ReactNoop . render ( < ParentComponent /> ) ;
210
193
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 ' +
220
201
'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' +
227
207
'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 ) ;
230
213
} ) ;
231
214
} ) ;
232
215
0 commit comments