Skip to content

Commit f82051d

Browse files
console test utils fix: match entire string, not just first letter (#28855)
Fixes issue where if the first letter of the expected string appeared anywhere in actual message, the assertion would pass, leading to false negatives. We should check the entire expected string. --------- Co-authored-by: Ricky <[email protected]>
1 parent 4ca20fd commit f82051d

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

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

+22
Original file line numberDiff line numberDiff line change
@@ -2129,6 +2129,28 @@ describe('ReactInternalTestUtils console assertions', () => {
21292129
`);
21302130
});
21312131

2132+
// @gate __DEV__
2133+
it('regression: checks entire string, not just the first letter', async () => {
2134+
const message = expectToThrowFailure(() => {
2135+
console.error('Message that happens to contain a "T"\n in div');
2136+
2137+
assertConsoleErrorDev([
2138+
'This is a completely different message that happens to start with "T"',
2139+
]);
2140+
});
2141+
expect(message).toMatchInlineSnapshot(`
2142+
"assertConsoleErrorDev(expected)
2143+
2144+
Unexpected error(s) recorded.
2145+
2146+
- Expected errors
2147+
+ Received errors
2148+
2149+
- This is a complete different message that happens to start with "T"
2150+
+ Message that happens to contain a "T" <component stack>"
2151+
`);
2152+
});
2153+
21322154
describe('global withoutStack', () => {
21332155
// @gate __DEV__
21342156
it('passes if errors without stack explicitly opt out', () => {

packages/internal-test-utils/consoleMock.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ export function createLogAssertion(
386386
expectedWithoutStack = expectedMessageOrArray[1].withoutStack;
387387
} else if (typeof expectedMessageOrArray === 'string') {
388388
// Should be in the form assert(['log']) or assert(['log'], {withoutStack: true})
389-
expectedMessage = replaceComponentStack(expectedMessageOrArray[0]);
389+
expectedMessage = replaceComponentStack(expectedMessageOrArray);
390390
if (consoleMethod === 'log') {
391391
expectedWithoutStack = true;
392392
} else {

0 commit comments

Comments
 (0)