@@ -13,6 +13,7 @@ describe('forwardRef', () => {
13
13
let React ;
14
14
let ReactNoop ;
15
15
let waitForAll ;
16
+ let assertConsoleErrorDev ;
16
17
17
18
beforeEach ( ( ) => {
18
19
jest . resetModules ( ) ;
@@ -21,6 +22,7 @@ describe('forwardRef', () => {
21
22
22
23
const InternalTestUtils = require ( 'internal-test-utils' ) ;
23
24
waitForAll = InternalTestUtils . waitForAll ;
25
+ assertConsoleErrorDev = InternalTestUtils . assertConsoleErrorDev ;
24
26
} ) ;
25
27
26
28
it ( 'should update refs when switching between children' , async ( ) => {
@@ -114,25 +116,31 @@ describe('forwardRef', () => {
114
116
} ) ;
115
117
116
118
it ( 'should warn if not provided a callback during creation' , ( ) => {
117
- expect ( ( ) => React . forwardRef ( undefined ) ) . toErrorDev (
118
- 'forwardRef requires a render function but was given undefined.' ,
119
+ React . forwardRef ( undefined ) ;
120
+ assertConsoleErrorDev (
121
+ [ 'forwardRef requires a render function but was given undefined.' ] ,
119
122
{ withoutStack : true } ,
120
123
) ;
121
- expect ( ( ) => React . forwardRef ( null ) ) . toErrorDev (
122
- 'forwardRef requires a render function but was given null.' ,
124
+
125
+ React . forwardRef ( null ) ;
126
+ assertConsoleErrorDev (
127
+ [ 'forwardRef requires a render function but was given null.' ] ,
123
128
{
124
129
withoutStack : true ,
125
130
} ,
126
131
) ;
127
- expect ( ( ) => React . forwardRef ( 'foo' ) ) . toErrorDev (
128
- 'forwardRef requires a render function but was given string.' ,
132
+
133
+ React . forwardRef ( 'foo' ) ;
134
+ assertConsoleErrorDev (
135
+ [ 'forwardRef requires a render function but was given string.' ] ,
129
136
{ withoutStack : true } ,
130
137
) ;
131
138
} ) ;
132
139
133
140
it ( 'should warn if no render function is provided' , ( ) => {
134
- expect ( React . forwardRef ) . toErrorDev (
135
- 'forwardRef requires a render function but was given undefined.' ,
141
+ React . forwardRef ( ) ;
142
+ assertConsoleErrorDev (
143
+ [ 'forwardRef requires a render function but was given undefined.' ] ,
136
144
{ withoutStack : true } ,
137
145
) ;
138
146
} ) ;
@@ -143,9 +151,12 @@ describe('forwardRef', () => {
143
151
}
144
152
renderWithDefaultProps . defaultProps = { } ;
145
153
146
- expect ( ( ) => React . forwardRef ( renderWithDefaultProps ) ) . toErrorDev (
147
- 'forwardRef render functions do not support defaultProps. ' +
148
- 'Did you accidentally pass a React component?' ,
154
+ React . forwardRef ( renderWithDefaultProps ) ;
155
+ assertConsoleErrorDev (
156
+ [
157
+ 'forwardRef render functions do not support defaultProps. ' +
158
+ 'Did you accidentally pass a React component?' ,
159
+ ] ,
149
160
{ withoutStack : true } ,
150
161
) ;
151
162
} ) ;
@@ -159,9 +170,12 @@ describe('forwardRef', () => {
159
170
it ( 'should warn if the render function provided does not use the forwarded ref parameter' , ( ) => {
160
171
const arityOfOne = props => < div { ...props } /> ;
161
172
162
- expect ( ( ) => React . forwardRef ( arityOfOne ) ) . toErrorDev (
163
- 'forwardRef render functions accept exactly two parameters: props and ref. ' +
164
- 'Did you forget to use the ref parameter?' ,
173
+ React . forwardRef ( arityOfOne ) ;
174
+ assertConsoleErrorDev (
175
+ [
176
+ 'forwardRef render functions accept exactly two parameters: props and ref. ' +
177
+ 'Did you forget to use the ref parameter?' ,
178
+ ] ,
165
179
{ withoutStack : true } ,
166
180
) ;
167
181
} ) ;
@@ -174,9 +188,12 @@ describe('forwardRef', () => {
174
188
it ( 'should warn if the render function provided expects to use more than two parameters' , ( ) => {
175
189
const arityOfThree = ( props , ref , x ) => < div { ...props } ref = { ref } x = { x } /> ;
176
190
177
- expect ( ( ) => React . forwardRef ( arityOfThree ) ) . toErrorDev (
178
- 'forwardRef render functions accept exactly two parameters: props and ref. ' +
179
- 'Any additional parameter will be undefined.' ,
191
+ React . forwardRef ( arityOfThree ) ;
192
+ assertConsoleErrorDev (
193
+ [
194
+ 'forwardRef render functions accept exactly two parameters: props and ref. ' +
195
+ 'Any additional parameter will be undefined.' ,
196
+ ] ,
180
197
{ withoutStack : true } ,
181
198
) ;
182
199
} ) ;
@@ -190,15 +207,16 @@ describe('forwardRef', () => {
190
207
< RefForwardingComponent />
191
208
</ p > ,
192
209
) ;
193
- await expect ( async ( ) => {
194
- await waitForAll ( [ ] ) ;
195
- } ) . toErrorDev (
210
+ await waitForAll ( [ ] ) ;
211
+ assertConsoleErrorDev ( [
196
212
'Each child in a list should have a unique "key" prop.' +
197
213
'\n\nCheck the top-level render call using <ForwardRef>. It was passed a child from ForwardRef. ' +
198
214
'See https://react.dev/link/warning-keys for more information.\n' +
199
215
' in span (at **)\n' +
200
- ' in ' ,
201
- ) ;
216
+ ( gate ( flags => flags . enableOwnerStacks )
217
+ ? ' in **/forwardRef-test.js:**:** (at **)'
218
+ : ' in p (at **)' ) ,
219
+ ] ) ;
202
220
} ) ;
203
221
204
222
it ( 'should use the inner function name for the stack' , async ( ) => {
@@ -210,16 +228,16 @@ describe('forwardRef', () => {
210
228
< RefForwardingComponent />
211
229
</ p > ,
212
230
) ;
213
- await expect ( async ( ) => {
214
- await waitForAll ( [ ] ) ;
215
- } ) . toErrorDev (
231
+
232
+ await waitForAll ( [ ] ) ;
233
+ assertConsoleErrorDev ( [
216
234
'Each child in a list should have a unique "key" prop.' +
217
235
'\n\nCheck the top-level render call using <ForwardRef(Inner)>. It was passed a child from ForwardRef(Inner). ' +
218
236
'See https://react.dev/link/warning-keys for more information.\n' +
219
237
' in span (at **)\n' +
220
238
' in Inner (at **)' +
221
239
( gate ( flags => flags . enableOwnerStacks ) ? '' : '\n in p (at **)' ) ,
222
- ) ;
240
+ ] ) ;
223
241
} ) ;
224
242
225
243
it ( 'should use the inner name in the stack' , async ( ) => {
@@ -233,16 +251,15 @@ describe('forwardRef', () => {
233
251
< RefForwardingComponent />
234
252
</ p > ,
235
253
) ;
236
- await expect ( async ( ) => {
237
- await waitForAll ( [ ] ) ;
238
- } ) . toErrorDev (
254
+ await waitForAll ( [ ] ) ;
255
+ assertConsoleErrorDev ( [
239
256
'Each child in a list should have a unique "key" prop.' +
240
257
'\n\nCheck the top-level render call using <ForwardRef(Inner)>. It was passed a child from ForwardRef(Inner). ' +
241
258
'See https://react.dev/link/warning-keys for more information.\n' +
242
259
' in span (at **)\n' +
243
260
' in Inner (at **)' +
244
261
( gate ( flags => flags . enableOwnerStacks ) ? '' : '\n in p (at **)' ) ,
245
- ) ;
262
+ ] ) ;
246
263
} ) ;
247
264
248
265
it ( 'can use the outer displayName in the stack' , async ( ) => {
@@ -255,16 +272,15 @@ describe('forwardRef', () => {
255
272
< RefForwardingComponent />
256
273
</ p > ,
257
274
) ;
258
- await expect ( async ( ) => {
259
- await waitForAll ( [ ] ) ;
260
- } ) . toErrorDev (
275
+ await waitForAll ( [ ] ) ;
276
+ assertConsoleErrorDev ( [
261
277
'Each child in a list should have a unique "key" prop.' +
262
278
'\n\nCheck the top-level render call using <Outer>. It was passed a child from Outer. ' +
263
279
'See https://react.dev/link/warning-keys for more information.\n' +
264
280
' in span (at **)\n' +
265
281
' in Outer (at **)' +
266
282
( gate ( flags => flags . enableOwnerStacks ) ? '' : '\n in p (at **)' ) ,
267
- ) ;
283
+ ] ) ;
268
284
} ) ;
269
285
270
286
it ( 'should prefer the inner name to the outer displayName in the stack' , async ( ) => {
@@ -279,16 +295,15 @@ describe('forwardRef', () => {
279
295
< RefForwardingComponent />
280
296
</ p > ,
281
297
) ;
282
- await expect ( async ( ) => {
283
- await waitForAll ( [ ] ) ;
284
- } ) . toErrorDev (
298
+ await waitForAll ( [ ] ) ;
299
+ assertConsoleErrorDev ( [
285
300
'Each child in a list should have a unique "key" prop.' +
286
301
'\n\nCheck the top-level render call using <Outer>. It was passed a child from Outer. ' +
287
302
'See https://react.dev/link/warning-keys for more information.\n' +
288
303
' in span (at **)\n' +
289
304
' in Inner (at **)' +
290
305
( gate ( flags => flags . enableOwnerStacks ) ? '' : '\n in p (at **)' ) ,
291
- ) ;
306
+ ] ) ;
292
307
} ) ;
293
308
294
309
it ( 'should not bailout if forwardRef is not wrapped in memo' , async ( ) => {
@@ -419,13 +434,12 @@ describe('forwardRef', () => {
419
434
} ) ;
420
435
421
436
it ( 'warns on forwardRef(memo(...))' , ( ) => {
422
- expect ( ( ) => {
423
- React . forwardRef (
424
- React . memo ( ( props , ref ) => {
425
- return null ;
426
- } ) ,
427
- ) ;
428
- } ) . toErrorDev (
437
+ React . forwardRef (
438
+ React . memo ( ( props , ref ) => {
439
+ return null ;
440
+ } ) ,
441
+ ) ;
442
+ assertConsoleErrorDev (
429
443
[
430
444
'forwardRef requires a render function but received a `memo` ' +
431
445
'component. Instead of forwardRef(memo(...)), use ' +
0 commit comments