diff --git a/packages/internal-test-utils/__tests__/ReactInternalTestUtils-test.js b/packages/internal-test-utils/__tests__/ReactInternalTestUtils-test.js index 3c101251868..b2474147786 100644 --- a/packages/internal-test-utils/__tests__/ReactInternalTestUtils-test.js +++ b/packages/internal-test-utils/__tests__/ReactInternalTestUtils-test.js @@ -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(() => { diff --git a/packages/internal-test-utils/consoleMock.js b/packages/internal-test-utils/consoleMock.js index ecb97b3a030..6c4ea86cf54 100644 --- a/packages/internal-test-utils/consoleMock.js +++ b/packages/internal-test-utils/consoleMock.js @@ -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) {