Skip to content

Commit 9433fe3

Browse files
authored
Fail tests if unasserted console calls contain undefined (#34191)
1 parent 0032b2a commit 9433fe3

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2169,6 +2169,29 @@ describe('ReactInternalTestUtils console assertions', () => {
21692169
+ Bye in div (at **)"
21702170
`);
21712171
});
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+
});
21722195
// @gate __DEV__
21732196
it('fails if only error does not contain a stack', () => {
21742197
const message = expectToThrowFailure(() => {

packages/internal-test-utils/consoleMock.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,9 @@ export function createLogAssertion(
382382

383383
// Main logic to check if log is expected, with the component stack.
384384
if (
385-
normalizedMessage === expectedMessage ||
386-
normalizedMessage.includes(expectedMessage)
385+
typeof expectedMessage === 'string' &&
386+
(normalizedMessage === expectedMessage ||
387+
normalizedMessage.includes(expectedMessage))
387388
) {
388389
if (isLikelyAComponentStack(normalizedMessage)) {
389390
if (expectedWithoutStack === true) {

0 commit comments

Comments
 (0)