Fix assertConsoleErrorDev on message mismatch with withoutStack: true (#29198)
## Summary ```js assertConsoleErrorDev([ ['Hello', {withoutStack: true}] ]) ``` now errors with a helpful diff message if the message mismatched. See first commit for previous behavior. ## How did you test this change? - `yarn test --watch packages/internal-test-utils/__tests__/ReactInternalTestUtils-test.js`
Sebastian Silbermann committed
May 21, 2024 at 18:48 UTC
5cc9f69a74ac42d55ad635229e9eaf129a1f0862
2 files changed
+32
-1
packages/internal-test-utils/__tests__/ReactInternalTestUtils-test.js
+26
@@ -2417,6 +2417,32 @@ describe('ReactInternalTestUtils console assertions', () => {
2417
If all errors should include the component stack, you may need to remove {withoutStack: true} from the assertConsoleErrorDev call."
2418
`);
2419
});
2420
+
2421
+ // @gate __DEV__
2422
+ it('fails with a helpful error message if the expected error message mismatches', () => {
2423
+ const message = expectToThrowFailure(() => {
2424
+ console.error('Bye\n in div');
2425
+ assertConsoleErrorDev([
2426
+ [
2427
+ 'Hello',
2428
+ {
2429
+ withoutStack: true,
2430
+ },
2431
+ ],
2432
+ ]);
2433
+ });
2434
+ expect(message).toMatchInlineSnapshot(`
2435
+ "assertConsoleErrorDev(expected)
2436
+
2437
+ Unexpected error(s) recorded.
2438
+
2439
+ - Expected errors
2440
+ + Received errors
2441
+
2442
+ - Hello
2443
+ + Bye <component stack>"
2444
+ `);
2445
+ });
2446
});
2447
2448
// @gate __DEV__
packages/internal-test-utils/consoleMock.js
+6
-1
@@ -464,7 +464,12 @@ export function createLogAssertion(
464
function printDiff() {
465
return `${diff(
466
expectedMessages
467
- .map(message => message.replace('\n', ' '))
467
+ .map(messageOrTuple => {
468
+ const message = Array.isArray(messageOrTuple)
469
+ ? messageOrTuple[0]
470
+ : messageOrTuple;
471
+ return message.replace('\n', ' ');
472
+ })
473
.join('\n'),
474
receivedLogs.map(message => message.replace('\n', ' ')).join('\n'),
475
{