Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8f92ea4

Browse files
authored
[assert helpers] forwardRef-test (#31843)
Starting to convert the rest of tests to the `assertConsoleTypeDev` helpers.
1 parent bd76ce5 commit 8f92ea4

File tree

2 files changed

+60
-45
lines changed

2 files changed

+60
-45
lines changed

packages/internal-test-utils/consoleMock.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ function normalizeCodeLocInfo(str) {
273273
// We strip that out in our normalization to make it look more like component stacks.
274274
name = name.slice(0, name.length - 7);
275275
}
276+
name = name.replace(/.*\/([^\/]+):\d+:\d+/, '**/$1:**:**');
276277
return '\n in ' + name + ' (at **)';
277278
});
278279
}

packages/react/src/__tests__/forwardRef-test.js

Lines changed: 59 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ describe('forwardRef', () => {
1313
let React;
1414
let ReactNoop;
1515
let waitForAll;
16+
let assertConsoleErrorDev;
1617

1718
beforeEach(() => {
1819
jest.resetModules();
@@ -21,6 +22,7 @@ describe('forwardRef', () => {
2122

2223
const InternalTestUtils = require('internal-test-utils');
2324
waitForAll = InternalTestUtils.waitForAll;
25+
assertConsoleErrorDev = InternalTestUtils.assertConsoleErrorDev;
2426
});
2527

2628
it('should update refs when switching between children', async () => {
@@ -114,25 +116,31 @@ describe('forwardRef', () => {
114116
});
115117

116118
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.'],
119122
{withoutStack: true},
120123
);
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.'],
123128
{
124129
withoutStack: true,
125130
},
126131
);
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.'],
129136
{withoutStack: true},
130137
);
131138
});
132139

133140
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.'],
136144
{withoutStack: true},
137145
);
138146
});
@@ -143,9 +151,12 @@ describe('forwardRef', () => {
143151
}
144152
renderWithDefaultProps.defaultProps = {};
145153

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+
],
149160
{withoutStack: true},
150161
);
151162
});
@@ -159,9 +170,12 @@ describe('forwardRef', () => {
159170
it('should warn if the render function provided does not use the forwarded ref parameter', () => {
160171
const arityOfOne = props => <div {...props} />;
161172

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+
],
165179
{withoutStack: true},
166180
);
167181
});
@@ -174,9 +188,12 @@ describe('forwardRef', () => {
174188
it('should warn if the render function provided expects to use more than two parameters', () => {
175189
const arityOfThree = (props, ref, x) => <div {...props} ref={ref} x={x} />;
176190

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+
],
180197
{withoutStack: true},
181198
);
182199
});
@@ -190,15 +207,16 @@ describe('forwardRef', () => {
190207
<RefForwardingComponent />
191208
</p>,
192209
);
193-
await expect(async () => {
194-
await waitForAll([]);
195-
}).toErrorDev(
210+
await waitForAll([]);
211+
assertConsoleErrorDev([
196212
'Each child in a list should have a unique "key" prop.' +
197213
'\n\nCheck the top-level render call using <ForwardRef>. It was passed a child from ForwardRef. ' +
198214
'See https://react.dev/link/warning-keys for more information.\n' +
199215
' in span (at **)\n' +
200-
' in ',
201-
);
216+
(gate(flags => flags.enableOwnerStacks)
217+
? ' in **/forwardRef-test.js:**:** (at **)'
218+
: ' in p (at **)'),
219+
]);
202220
});
203221

204222
it('should use the inner function name for the stack', async () => {
@@ -210,16 +228,16 @@ describe('forwardRef', () => {
210228
<RefForwardingComponent />
211229
</p>,
212230
);
213-
await expect(async () => {
214-
await waitForAll([]);
215-
}).toErrorDev(
231+
232+
await waitForAll([]);
233+
assertConsoleErrorDev([
216234
'Each child in a list should have a unique "key" prop.' +
217235
'\n\nCheck the top-level render call using <ForwardRef(Inner)>. It was passed a child from ForwardRef(Inner). ' +
218236
'See https://react.dev/link/warning-keys for more information.\n' +
219237
' in span (at **)\n' +
220238
' in Inner (at **)' +
221239
(gate(flags => flags.enableOwnerStacks) ? '' : '\n in p (at **)'),
222-
);
240+
]);
223241
});
224242

225243
it('should use the inner name in the stack', async () => {
@@ -233,16 +251,15 @@ describe('forwardRef', () => {
233251
<RefForwardingComponent />
234252
</p>,
235253
);
236-
await expect(async () => {
237-
await waitForAll([]);
238-
}).toErrorDev(
254+
await waitForAll([]);
255+
assertConsoleErrorDev([
239256
'Each child in a list should have a unique "key" prop.' +
240257
'\n\nCheck the top-level render call using <ForwardRef(Inner)>. It was passed a child from ForwardRef(Inner). ' +
241258
'See https://react.dev/link/warning-keys for more information.\n' +
242259
' in span (at **)\n' +
243260
' in Inner (at **)' +
244261
(gate(flags => flags.enableOwnerStacks) ? '' : '\n in p (at **)'),
245-
);
262+
]);
246263
});
247264

248265
it('can use the outer displayName in the stack', async () => {
@@ -255,16 +272,15 @@ describe('forwardRef', () => {
255272
<RefForwardingComponent />
256273
</p>,
257274
);
258-
await expect(async () => {
259-
await waitForAll([]);
260-
}).toErrorDev(
275+
await waitForAll([]);
276+
assertConsoleErrorDev([
261277
'Each child in a list should have a unique "key" prop.' +
262278
'\n\nCheck the top-level render call using <Outer>. It was passed a child from Outer. ' +
263279
'See https://react.dev/link/warning-keys for more information.\n' +
264280
' in span (at **)\n' +
265281
' in Outer (at **)' +
266282
(gate(flags => flags.enableOwnerStacks) ? '' : '\n in p (at **)'),
267-
);
283+
]);
268284
});
269285

270286
it('should prefer the inner name to the outer displayName in the stack', async () => {
@@ -279,16 +295,15 @@ describe('forwardRef', () => {
279295
<RefForwardingComponent />
280296
</p>,
281297
);
282-
await expect(async () => {
283-
await waitForAll([]);
284-
}).toErrorDev(
298+
await waitForAll([]);
299+
assertConsoleErrorDev([
285300
'Each child in a list should have a unique "key" prop.' +
286301
'\n\nCheck the top-level render call using <Outer>. It was passed a child from Outer. ' +
287302
'See https://react.dev/link/warning-keys for more information.\n' +
288303
' in span (at **)\n' +
289304
' in Inner (at **)' +
290305
(gate(flags => flags.enableOwnerStacks) ? '' : '\n in p (at **)'),
291-
);
306+
]);
292307
});
293308

294309
it('should not bailout if forwardRef is not wrapped in memo', async () => {
@@ -419,13 +434,12 @@ describe('forwardRef', () => {
419434
});
420435

421436
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(
429443
[
430444
'forwardRef requires a render function but received a `memo` ' +
431445
'component. Instead of forwardRef(memo(...)), use ' +

0 commit comments

Comments
 (0)