Fail tests if unasserted console calls contain undefined (#34191)

This commit is contained in:
Sebastian "Sebbie" Silbermann 2025-08-13 08:48:04 +02:00 committed by GitHub
parent 0032b2a3ee
commit 9433fe357a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 2 deletions

View File

@ -2169,6 +2169,29 @@ describe('ReactInternalTestUtils console assertions', () => {
+ Bye in div (at **)"
`);
});
// @gate __DEV__
it('fails if last received error containing "undefined" is not included', () => {
const message = expectToThrowFailure(() => {
console.error('Hi');
console.error(
"TypeError: Cannot read properties of undefined (reading 'stack')\n" +
' in Foo (at **)'
);
assertConsoleErrorDev([['Hi', {withoutStack: true}]]);
});
expect(message).toMatchInlineSnapshot(`
"assertConsoleErrorDev(expected)
Unexpected error(s) recorded.
- Expected errors
+ Received errors
Hi
+ TypeError: Cannot read properties of undefined (reading 'stack') in Foo (at **)"
`);
});
// @gate __DEV__
it('fails if only error does not contain a stack', () => {
const message = expectToThrowFailure(() => {

View File

@ -382,8 +382,9 @@ export function createLogAssertion(
// Main logic to check if log is expected, with the component stack.
if (
normalizedMessage === expectedMessage ||
normalizedMessage.includes(expectedMessage)
typeof expectedMessage === 'string' &&
(normalizedMessage === expectedMessage ||
normalizedMessage.includes(expectedMessage))
) {
if (isLikelyAComponentStack(normalizedMessage)) {
if (expectedWithoutStack === true) {