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]>
This commit is contained in:
Andrew Clark
2024-04-17 13:04:54 -04:00
committed by GitHub
co-authored by Ricky
parent 533df973db
commit a3fe7a2c45
2 changed files with 23 additions and 1 deletions
@@ -2129,6 +2129,28 @@ describe('ReactInternalTestUtils console assertions', () => {
`);
});
// @gate __DEV__
it('regression: checks entire string, not just the first letter', async () => {
const message = expectToThrowFailure(() => {
console.error('Message that happens to contain a "T"\n in div');
assertConsoleErrorDev([
'This is a completely different message that happens to start with "T"',
]);
});
expect(message).toMatchInlineSnapshot(`
"assertConsoleErrorDev(expected)
Unexpected error(s) recorded.
- Expected errors
+ Received errors
- This is a complete different message that happens to start with "T"
+ Message that happens to contain a "T" <component stack>"
`);
});
describe('global withoutStack', () => {
// @gate __DEV__
it('passes if errors without stack explicitly opt out', () => {
+1 -1
View File
@@ -386,7 +386,7 @@ export function createLogAssertion(
expectedWithoutStack = expectedMessageOrArray[1].withoutStack;
} else if (typeof expectedMessageOrArray === 'string') {
// Should be in the form assert(['log']) or assert(['log'], {withoutStack: true})
expectedMessage = replaceComponentStack(expectedMessageOrArray[0]);
expectedMessage = replaceComponentStack(expectedMessageOrArray);
if (consoleMethod === 'log') {
expectedWithoutStack = true;
} else {