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 <rickhanlonii@gmail.com>
Andrew Clark committed
Apr 17, 2024 at 13:04 UTC
f82051d7abf9a2137f62cb84f0dd3618397951bc
2 files changed
+23
-1
packages/internal-test-utils/__tests__/ReactInternalTestUtils-test.js
+22
@@ -2129,6 +2129,28 @@ describe('ReactInternalTestUtils console assertions', () => {
2129
`);
2130
});
2131
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
+
2154
describe('global withoutStack', () => {
2155
// @gate __DEV__
2156
it('passes if errors without stack explicitly opt out', () => {
packages/internal-test-utils/consoleMock.js
+1
-1
@@ -386,7 +386,7 @@ export function createLogAssertion(
386
expectedWithoutStack = expectedMessageOrArray[1].withoutStack;
387
} else if (typeof expectedMessageOrArray === 'string') {
388
// Should be in the form assert(['log']) or assert(['log'], {withoutStack: true})
389
- expectedMessage = replaceComponentStack(expectedMessageOrArray[0]);
389
+ expectedMessage = replaceComponentStack(expectedMessageOrArray);
390
if (consoleMethod === 'log') {
391
expectedWithoutStack = true;
392
} else {