Skip to content

Commit

Permalink
Fix assertConsoleErrorDev on message mismatch with withoutStack: true (
Browse files Browse the repository at this point in the history
…#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`
  • Loading branch information
eps1lon committed May 21, 2024
1 parent 7621466 commit 5cc9f69
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down
7 changes: 6 additions & 1 deletion packages/internal-test-utils/consoleMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
{
Expand Down

0 comments on commit 5cc9f69

Please sign in to comment.