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`
This commit is contained in:
Sebastian Silbermann
2024-05-21 12:48:41 -04:00
committed by GitHub
parent 4f21085fce
commit 76405a2fda
2 changed files with 32 additions and 1 deletions
@@ -2417,6 +2417,32 @@ describe('ReactInternalTestUtils console assertions', () => {
If all errors should include the component stack, you may need to remove {withoutStack: true} from the assertConsoleErrorDev call."
`);
});
// @gate __DEV__
it('fails with a helpful error message if the expected error message mismatches', () => {
const message = expectToThrowFailure(() => {
console.error('Bye\n in div');
assertConsoleErrorDev([
[
'Hello',
{
withoutStack: true,
},
],
]);
});
expect(message).toMatchInlineSnapshot(`
"assertConsoleErrorDev(expected)
Unexpected error(s) recorded.
- Expected errors
+ Received errors
- Hello
+ Bye <component stack>"
`);
});
});
// @gate __DEV__
+6 -1
View File
@@ -464,7 +464,12 @@ export function createLogAssertion(
function printDiff() {
return `${diff(
expectedMessages
.map(message => message.replace('\n', ' '))
.map(messageOrTuple => {
const message = Array.isArray(messageOrTuple)
? messageOrTuple[0]
: messageOrTuple;
return message.replace('\n', ' ');
})
.join('\n'),
receivedLogs.map(message => message.replace('\n', ' ')).join('\n'),
{