Fail tests if unasserted console calls contain `undefined` (#34191)
Sebastian "Sebbie" Silbermann committed
Aug 13, 2025 at 08:48 UTC
9433fe357aa5df277f0e509c1b8cf975afe9ec9a
2 files changed
+26
-2
packages/internal-test-utils/__tests__/ReactInternalTestUtils-test.js
+23
@@ -2169,6 +2169,29 @@ describe('ReactInternalTestUtils console assertions', () => {
2169
+ Bye in div (at **)"
2170
`);
2171
});
2172
+
2173
+ // @gate __DEV__
2174
+ it('fails if last received error containing "undefined" is not included', () => {
2175
+ const message = expectToThrowFailure(() => {
2176
+ console.error('Hi');
2177
+ console.error(
2178
+ "TypeError: Cannot read properties of undefined (reading 'stack')\n" +
2179
+ ' in Foo (at **)'
2180
+ );
2181
+ assertConsoleErrorDev([['Hi', {withoutStack: true}]]);
2182
+ });
2183
+ expect(message).toMatchInlineSnapshot(`
2184
+ "assertConsoleErrorDev(expected)
2185
+
2186
+ Unexpected error(s) recorded.
2187
+
2188
+ - Expected errors
2189
+ + Received errors
2190
+
2191
+ Hi
2192
+ + TypeError: Cannot read properties of undefined (reading 'stack') in Foo (at **)"
2193
+ `);
2194
+ });
2195
// @gate __DEV__
2196
it('fails if only error does not contain a stack', () => {
2197
const message = expectToThrowFailure(() => {
packages/internal-test-utils/consoleMock.js
+3
-2
@@ -382,8 +382,9 @@ export function createLogAssertion(
382
383
// Main logic to check if log is expected, with the component stack.
384
if (
385
- normalizedMessage === expectedMessage ||
386
- normalizedMessage.includes(expectedMessage)
385
+ typeof expectedMessage === 'string' &&
386
+ (normalizedMessage === expectedMessage ||
387
+ normalizedMessage.includes(expectedMessage))
388
) {
389
if (isLikelyAComponentStack(normalizedMessage)) {
390
if (expectedWithoutStack === true) {