Skip to content

Commit e66ef64

Browse files
authored
[tests] remove withoutStack from assertConsole helpers (facebook#35498)
Stacked on facebook#35497 ----- Now that the assert helpers require a component stack, we don't need the `{withoutStack: true}` option.
1 parent 8c34556 commit e66ef64

File tree

43 files changed

+874
-2055
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+874
-2055
lines changed

packages/internal-test-utils/__tests__/ReactInternalTestUtils-test.js

Lines changed: 12 additions & 643 deletions
Large diffs are not rendered by default.

packages/internal-test-utils/consoleMock.js

Lines changed: 3 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -290,26 +290,11 @@ export function createLogAssertion(
290290
}
291291
}
292292

293-
const withoutStack = options.withoutStack;
294-
295-
if (consoleMethod === 'log' && withoutStack !== undefined) {
296-
throwFormattedError(
297-
`Do not pass withoutStack to assertConsoleLogDev, console.log does not have component stacks.`,
298-
);
299-
} else if (withoutStack !== undefined && withoutStack !== true) {
300-
throwFormattedError(
301-
`The second argument must be {withoutStack: true}.` +
302-
`\n\nInstead received ${JSON.stringify(options)}.`,
303-
);
304-
}
305-
306293
const observedLogs = clearObservedErrors();
307294
const receivedLogs = [];
308295
const missingExpectedLogs = Array.from(expectedMessages);
309296

310297
const unexpectedLogs = [];
311-
const unexpectedMissingComponentStack = [];
312-
const unexpectedIncludingComponentStack = [];
313298
const unexpectedMissingErrorStack = [];
314299
const unexpectedIncludingErrorStack = [];
315300
const logsMismatchingFormat = [];
@@ -334,72 +319,12 @@ export function createLogAssertion(
334319
}
335320

336321
let expectedMessage;
337-
let expectedWithoutStack;
338322
const expectedMessageOrArray = expectedMessages[index];
339-
if (
340-
expectedMessageOrArray != null &&
341-
Array.isArray(expectedMessageOrArray)
342-
) {
343-
// Should be in the local form assert([['log', {withoutStack: true}]])
344-
345-
// Some validations for common mistakes.
346-
if (expectedMessageOrArray.length === 1) {
347-
throwFormattedError(
348-
`Did you forget to remove the array around the log?` +
349-
`\n\nThe expected message for ${matcherName}() must be a string or an array of length 2, but there's only one item in the array. If this is intentional, remove the extra array.`,
350-
);
351-
} else if (expectedMessageOrArray.length !== 2) {
352-
throwFormattedError(
353-
`The expected message for ${matcherName}() must be a string or an array of length 2. ` +
354-
`Instead received ${expectedMessageOrArray}.`,
355-
);
356-
} else if (consoleMethod === 'log') {
357-
// We don't expect any console.log calls to have a stack.
358-
throwFormattedError(
359-
`Do not pass withoutStack to assertConsoleLogDev logs, console.log does not have component stacks.`,
360-
);
361-
}
362-
363-
// Format is correct, check the values.
364-
const currentExpectedMessage = expectedMessageOrArray[0];
365-
const currentExpectedOptions = expectedMessageOrArray[1];
366-
if (
367-
typeof currentExpectedMessage !== 'string' ||
368-
typeof currentExpectedOptions !== 'object' ||
369-
currentExpectedOptions.withoutStack !== true
370-
) {
371-
throwFormattedError(
372-
`Log entries that are arrays must be of the form [string, {withoutStack: true}]` +
373-
`\n\nInstead received [${typeof currentExpectedMessage}, ${JSON.stringify(
374-
currentExpectedOptions,
375-
)}].`,
376-
);
377-
}
378-
379-
expectedMessage = normalizeExpectedMessage(currentExpectedMessage);
380-
expectedWithoutStack = expectedMessageOrArray[1].withoutStack;
381-
} else if (typeof expectedMessageOrArray === 'string') {
323+
if (typeof expectedMessageOrArray === 'string') {
382324
expectedMessage = normalizeExpectedMessage(expectedMessageOrArray);
383-
// withoutStack: inherit from global option - simplify when withoutStack is removed.
384-
if (consoleMethod === 'log') {
385-
expectedWithoutStack = true;
386-
} else {
387-
expectedWithoutStack = withoutStack;
388-
}
389-
} else if (
390-
typeof expectedMessageOrArray === 'object' &&
391-
expectedMessageOrArray != null &&
392-
expectedMessageOrArray.withoutStack != null
393-
) {
394-
// Special case for common case of a wrong withoutStack value.
395-
throwFormattedError(
396-
`Did you forget to wrap a log with withoutStack in an array?` +
397-
`\n\nThe expected message for ${matcherName}() must be a string or an array of length 2.` +
398-
`\n\nInstead received ${JSON.stringify(expectedMessageOrArray)}.`,
399-
);
400325
} else if (expectedMessageOrArray != null) {
401326
throwFormattedError(
402-
`The expected message for ${matcherName}() must be a string or an array of length 2. ` +
327+
`The expected message for ${matcherName}() must be a string. ` +
403328
`Instead received ${JSON.stringify(expectedMessageOrArray)}.`,
404329
);
405330
}
@@ -499,18 +424,6 @@ export function createLogAssertion(
499424
}
500425

501426
if (matchesExpectedMessage) {
502-
// withoutStack: Check for unexpected/missing component stacks.
503-
// These checks can be simplified when withoutStack is removed.
504-
if (isLikelyAComponentStack(normalizedMessage)) {
505-
if (expectedWithoutStack === true && !hasErrorStack) {
506-
// Only report unexpected component stack if it's not an error stack
507-
// (error stacks look like component stacks after normalization)
508-
unexpectedIncludingComponentStack.push(normalizedMessage);
509-
}
510-
} else if (expectedWithoutStack !== true && !expectsErrorStack) {
511-
unexpectedMissingComponentStack.push(normalizedMessage);
512-
}
513-
514427
// Check for unexpected/missing error stacks
515428
if (hasErrorStack && !expectsErrorStack) {
516429
// Error stack is present but \n in <stack> was not in the expected message
@@ -538,12 +451,7 @@ export function createLogAssertion(
538451
function printDiff() {
539452
return `${diff(
540453
expectedMessages
541-
.map(messageOrTuple => {
542-
const message = Array.isArray(messageOrTuple)
543-
? messageOrTuple[0]
544-
: messageOrTuple;
545-
return message.replace('\n', ' ');
546-
})
454+
.map(message => message.replace('\n', ' '))
547455
.join('\n'),
548456
receivedLogs.map(message => message.replace('\n', ' ')).join('\n'),
549457
{
@@ -582,36 +490,6 @@ export function createLogAssertion(
582490
);
583491
}
584492

585-
// Any logs that include a component stack but shouldn't.
586-
if (unexpectedIncludingComponentStack.length > 0) {
587-
throwFormattedError(
588-
`${unexpectedIncludingComponentStack
589-
.map(
590-
stack =>
591-
`Unexpected component stack for:\n ${printReceived(stack)}`,
592-
)
593-
.join(
594-
'\n\n',
595-
)}\n\nIf this ${logName()} should include a component stack, remove {withoutStack: true} from this ${logName()}.` +
596-
`\nIf all ${logName()}s should include the component stack, you may need to remove {withoutStack: true} from the ${matcherName} call.`,
597-
);
598-
}
599-
600-
// Any logs that are missing a component stack without withoutStack.
601-
if (unexpectedMissingComponentStack.length > 0) {
602-
throwFormattedError(
603-
`${unexpectedMissingComponentStack
604-
.map(
605-
stack =>
606-
`Missing component stack for:\n ${printReceived(stack)}`,
607-
)
608-
.join(
609-
'\n\n',
610-
)}\n\nIf this ${logName()} should omit a component stack, pass [log, {withoutStack: true}].` +
611-
`\nIf all ${logName()}s should omit the component stack, add {withoutStack: true} to the ${matcherName} call.`,
612-
);
613-
}
614-
615493
// Any logs that include an error stack trace but \n in <stack> wasn't expected.
616494
if (unexpectedIncludingErrorStack.length > 0) {
617495
throwFormattedError(

packages/react-client/src/__tests__/ReactFlight-test.js

Lines changed: 58 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,16 +1514,13 @@ describe('ReactFlight', () => {
15141514
},
15151515
};
15161516
const transport = ReactNoopFlightServer.render(<input value={obj} />);
1517-
assertConsoleErrorDev(
1518-
[
1519-
'Only plain objects can be passed to Client Components from Server Components. ' +
1520-
'Objects with toJSON methods are not supported. ' +
1521-
'Convert it manually to a simple value before passing it to props.\n' +
1522-
' <input value={{toJSON: ...}}>\n' +
1523-
' ^^^^^^^^^^^^^^^',
1524-
],
1525-
{withoutStack: true},
1526-
);
1517+
assertConsoleErrorDev([
1518+
'Only plain objects can be passed to Client Components from Server Components. ' +
1519+
'Objects with toJSON methods are not supported. ' +
1520+
'Convert it manually to a simple value before passing it to props.\n' +
1521+
' <input value={{toJSON: ...}}>\n' +
1522+
' ^^^^^^^^^^^^^^^',
1523+
]);
15271524

15281525
ReactNoopFlightClient.read(transport);
15291526
assertConsoleErrorDev([
@@ -1545,14 +1542,11 @@ describe('ReactFlight', () => {
15451542
const transport = ReactNoopFlightServer.render(
15461543
<div>Womp womp: {new MyError('spaghetti')}</div>,
15471544
);
1548-
assertConsoleErrorDev(
1549-
[
1550-
'Error objects cannot be rendered as text children. Try formatting it using toString().\n' +
1551-
' <div>Womp womp: {Error}</div>\n' +
1552-
' ^^^^^^^',
1553-
],
1554-
{withoutStack: true},
1555-
);
1545+
assertConsoleErrorDev([
1546+
'Error objects cannot be rendered as text children. Try formatting it using toString().\n' +
1547+
' <div>Womp womp: {Error}</div>\n' +
1548+
' ^^^^^^^',
1549+
]);
15561550

15571551
ReactNoopFlightClient.read(transport);
15581552
assertConsoleErrorDev([
@@ -1565,15 +1559,12 @@ describe('ReactFlight', () => {
15651559

15661560
it('should warn in DEV if a special object is passed to a host component', () => {
15671561
const transport = ReactNoopFlightServer.render(<input value={Math} />);
1568-
assertConsoleErrorDev(
1569-
[
1570-
'Only plain objects can be passed to Client Components from Server Components. ' +
1571-
'Math objects are not supported.\n' +
1572-
' <input value={Math}>\n' +
1573-
' ^^^^^^',
1574-
],
1575-
{withoutStack: true},
1576-
);
1562+
assertConsoleErrorDev([
1563+
'Only plain objects can be passed to Client Components from Server Components. ' +
1564+
'Math objects are not supported.\n' +
1565+
' <input value={Math}>\n' +
1566+
' ^^^^^^',
1567+
]);
15771568

15781569
ReactNoopFlightClient.read(transport);
15791570
assertConsoleErrorDev([
@@ -1589,15 +1580,12 @@ describe('ReactFlight', () => {
15891580
const transport = ReactNoopFlightServer.render(
15901581
<input value={{[Symbol.iterator]: {}}} />,
15911582
);
1592-
assertConsoleErrorDev(
1593-
[
1594-
'Only plain objects can be passed to Client Components from Server Components. ' +
1595-
'Objects with symbol properties like Symbol.iterator are not supported.\n' +
1596-
' <input value={{}}>\n' +
1597-
' ^^^^',
1598-
],
1599-
{withoutStack: true},
1600-
);
1583+
assertConsoleErrorDev([
1584+
'Only plain objects can be passed to Client Components from Server Components. ' +
1585+
'Objects with symbol properties like Symbol.iterator are not supported.\n' +
1586+
' <input value={{}}>\n' +
1587+
' ^^^^',
1588+
]);
16011589

16021590
ReactNoopFlightClient.read(transport);
16031591
assertConsoleErrorDev([
@@ -1620,16 +1608,13 @@ describe('ReactFlight', () => {
16201608
}
16211609
const Client = clientReference(ClientImpl);
16221610
const transport = ReactNoopFlightServer.render(<Client value={obj} />);
1623-
assertConsoleErrorDev(
1624-
[
1625-
'Only plain objects can be passed to Client Components from Server Components. ' +
1626-
'Objects with toJSON methods are not supported. ' +
1627-
'Convert it manually to a simple value before passing it to props.\n' +
1628-
' <... value={{toJSON: ...}}>\n' +
1629-
' ^^^^^^^^^^^^^^^',
1630-
],
1631-
{withoutStack: true},
1632-
);
1611+
assertConsoleErrorDev([
1612+
'Only plain objects can be passed to Client Components from Server Components. ' +
1613+
'Objects with toJSON methods are not supported. ' +
1614+
'Convert it manually to a simple value before passing it to props.\n' +
1615+
' <... value={{toJSON: ...}}>\n' +
1616+
' ^^^^^^^^^^^^^^^',
1617+
]);
16331618

16341619
ReactNoopFlightClient.read(transport);
16351620
assertConsoleErrorDev([
@@ -1655,16 +1640,13 @@ describe('ReactFlight', () => {
16551640
const transport = ReactNoopFlightServer.render(
16561641
<Client>Current date: {obj}</Client>,
16571642
);
1658-
assertConsoleErrorDev(
1659-
[
1660-
'Only plain objects can be passed to Client Components from Server Components. ' +
1661-
'Objects with toJSON methods are not supported. ' +
1662-
'Convert it manually to a simple value before passing it to props.\n' +
1663-
' <>Current date: {{toJSON: ...}}</>\n' +
1664-
' ^^^^^^^^^^^^^^^',
1665-
],
1666-
{withoutStack: true},
1667-
);
1643+
assertConsoleErrorDev([
1644+
'Only plain objects can be passed to Client Components from Server Components. ' +
1645+
'Objects with toJSON methods are not supported. ' +
1646+
'Convert it manually to a simple value before passing it to props.\n' +
1647+
' <>Current date: {{toJSON: ...}}</>\n' +
1648+
' ^^^^^^^^^^^^^^^',
1649+
]);
16681650

16691651
ReactNoopFlightClient.read(transport);
16701652
assertConsoleErrorDev([
@@ -1683,15 +1665,12 @@ describe('ReactFlight', () => {
16831665
}
16841666
const Client = clientReference(ClientImpl);
16851667
const transport = ReactNoopFlightServer.render(<Client value={Math} />);
1686-
assertConsoleErrorDev(
1687-
[
1688-
'Only plain objects can be passed to Client Components from Server Components. ' +
1689-
'Math objects are not supported.\n' +
1690-
' <... value={Math}>\n' +
1691-
' ^^^^^^',
1692-
],
1693-
{withoutStack: true},
1694-
);
1668+
assertConsoleErrorDev([
1669+
'Only plain objects can be passed to Client Components from Server Components. ' +
1670+
'Math objects are not supported.\n' +
1671+
' <... value={Math}>\n' +
1672+
' ^^^^^^',
1673+
]);
16951674

16961675
ReactNoopFlightClient.read(transport);
16971676
assertConsoleErrorDev([
@@ -1712,15 +1691,12 @@ describe('ReactFlight', () => {
17121691
const transport = ReactNoopFlightServer.render(
17131692
<Client value={{[Symbol.iterator]: {}}} />,
17141693
);
1715-
assertConsoleErrorDev(
1716-
[
1717-
'Only plain objects can be passed to Client Components from Server Components. ' +
1718-
'Objects with symbol properties like Symbol.iterator are not supported.\n' +
1719-
' <... value={{}}>\n' +
1720-
' ^^^^',
1721-
],
1722-
{withoutStack: true},
1723-
);
1694+
assertConsoleErrorDev([
1695+
'Only plain objects can be passed to Client Components from Server Components. ' +
1696+
'Objects with symbol properties like Symbol.iterator are not supported.\n' +
1697+
' <... value={{}}>\n' +
1698+
' ^^^^',
1699+
]);
17241700

17251701
ReactNoopFlightClient.read(transport);
17261702

@@ -1744,13 +1720,10 @@ describe('ReactFlight', () => {
17441720
ReactNoopFlightClient.read(transport);
17451721

17461722
assertConsoleErrorDev([
1747-
[
1748-
'Only plain objects can be passed to Client Components from Server Components. ' +
1749-
'Objects with symbol properties like Symbol.iterator are not supported.\n' +
1750-
' <... value={{}}>\n' +
1751-
' ^^^^',
1752-
{withoutStack: true},
1753-
],
1723+
'Only plain objects can be passed to Client Components from Server Components. ' +
1724+
'Objects with symbol properties like Symbol.iterator are not supported.\n' +
1725+
' <... value={{}}>\n' +
1726+
' ^^^^',
17541727
'Only plain objects can be passed to Client Components from Server Components. ' +
17551728
'Objects with symbol properties like Symbol.iterator are not supported.\n' +
17561729
' <... value={{}}>\n' +
@@ -1769,13 +1742,10 @@ describe('ReactFlight', () => {
17691742
);
17701743
ReactNoopFlightClient.read(transport);
17711744
assertConsoleErrorDev([
1772-
[
1773-
'Only plain objects can be passed to Client Components from Server Components. ' +
1774-
'Math objects are not supported.\n' +
1775-
' [..., Math, <h1/>]\n' +
1776-
' ^^^^',
1777-
{withoutStack: true},
1778-
],
1745+
'Only plain objects can be passed to Client Components from Server Components. ' +
1746+
'Math objects are not supported.\n' +
1747+
' [..., Math, <h1/>]\n' +
1748+
' ^^^^',
17791749
'Only plain objects can be passed to Client Components from Server Components. ' +
17801750
'Math objects are not supported.\n' +
17811751
' [..., Math, <h1/>]\n' +

0 commit comments

Comments
 (0)