-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update toEqual failure message #7325
Conversation
2 | ||
Received: | ||
1 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This report which doesn’t display expected and received values is evidence for request to remove && !oneline
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Making the change you suggested did not have any effect on this snapshot. I believe that is because jest-diff
returns null
if the type of the expected value is number
and two numbers are being compared for this test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CI failed because this snapshot (which now has added lines we wanted) wasn’t updated in committed files:
Expected: 2
Received: 1
packages/expect/src/matchers.js
Outdated
` ${printReceived(received)}` + | ||
matcherHint('.toEqual', undefined, undefined, { | ||
isNot: this.isNot, | ||
}) + | ||
(diffString && !oneline ? `\n\nDifference:\n\n${diffString}` : '') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please remove && !oneline
so the report displays expected and received values even if one line.
@phapp88 Thank you. Please forgive the delay to review your work. I requested one change. |
Codecov Report
@@ Coverage Diff @@
## master #7325 +/- ##
==========================================
- Coverage 66.64% 66.64% -0.01%
==========================================
Files 241 241
Lines 9345 9344 -1
Branches 6 5 -1
==========================================
- Hits 6228 6227 -1
Misses 3114 3114
Partials 3 3
Continue to review full report at Codecov.
|
Yes, my bad, expect('received').toEqual('expected'); // diffString is string
expect(1).toEqual(0); // diffString is null
expect(true).toEqual(false); // diffString is null Therefore, it looks like we need the following EDIT in the second return (
matcherHint('.toEqual', undefined, undefined, {
isNot: this.isNot,
}) +
'\n\n' +
(diffString
? `Difference:\n\n${diffString}`
: `Expected: ${printExpected(expected)}\n` +
`Received: ${printReceived(received)}`)
); |
<green>-0</> | ||
Received: | ||
<red>0</> | ||
|
||
Difference: | ||
|
||
<dim>Compared values have no visual difference.</>" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is because jest-diff
use strict equality ===
operator when Object.is
might be better.
That would be a separate pull request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should land that on master, then rebase this, right? To ensure the message is good
<green>ArrayContaining [1, 2]</> | ||
Received: | ||
<red>1</> | ||
|
||
Difference: | ||
|
||
Comparing two different types of values. Expected <green>array</> but received <red>number</>." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be a problem though.
@phapp88 Thank you for your patient follow through. It might be until Wednesday before my mental slow cooker has suggestion what to do about edge cases from |
@pedrottimark No problem. I'd be willing to submit other pull requests related to |
Here is another condition to display Expected and Received values when
EDIT incorrect: RegExp because node.green displays Not anchored at start because of escape sequences for green and red color. const diffString = diff(expected, received, {expand: this.expand});
return (
matcherHint('.toEqual', undefined, undefined, {
isNot: this.isNot,
}) +
'\n\n' +
(diffString && diffString.includes('- Expect')
? `Difference:\n\n${diffString}`
: `Expected: ${printExpected(expected)}\n` +
`Received: ${printReceived(received)}`)
); The memory is coming back: these tedious details are why this matcher was left until later :) |
expect(1).toEqual(0); // diffString is null
expect(true).toEqual(false); // diffString is null That's a bug, no? |
@SimenB hard to say if bug or feature:
I got stuck at the beginning of 2018 thinking about consequences of changing its contract, so now am looking for ways to move forward on By the way, can you give a second opinion about https://github.com/facebook/jest/blob/master/packages/jest-diff/src/index.js#L53-L55 I think instead of |
Yes 🙂 |
It's been in Node since v4? https://node.green/#ES2015-built-in-extensions-String-prototype-methods-String-prototype-includes |
You are correct. My bad, I think I misunderstood a table row under well-known symbols. |
|
||
Difference: | ||
|
||
<dim>Compared values have no visual difference.</>" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Super that the work-around fixed this existing problem.
|
||
Difference: | ||
|
||
Comparing two different types of values. Expected <green>array</> but received <red>number</>." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bravo, also fixed this problem.
`Received:\n` + | ||
` ${printReceived(received)}` + | ||
(diffString && !oneline ? `\n\nDifference:\n\n${diffString}` : '') | ||
(diffString && diffString.includes('- Expect') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pedrottimark it's not particular fault of this PR, but generally why do we show diffString in this branch, but not on the other? Looking at the snapshots and seeing the diff sometimes shown for one-liners and sometimes not is weird :|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question about the asymmetry: first branch means that .not.toBe
fails, therefore not likely to have differences. For some other matchers the expected might be asymmetric.
<red>+ Received</> | ||
|
||
<green>- apple</> | ||
<red>+ banana</>" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't this be shown as:
Expected: apple
Received: banana
?
It's weird seeing the diff for single lines, when Expected/Received
fits better for this purpose
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the reason is maybe in the future to display substring differences.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of the diff looks better, so I'm keen on getting this to master and iterate on the edge cases causing showing the full "Difference: " message for single-line values
@phapp88 thank you for contributing to Jest |
* Update toEqual failure message * Update changelog * Update failing snapshots * Show one line reports * Update failure message when diffString is null * Update failing snapshot * Update failure message for jest-diff edge cases
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Summary
This pull request updates the failure message returned by toEqual so that it is identical to the improved message shown in #7105. The changes made are similar to the changes made in #7224.
Test plan
The matcher is tested with a snapshot test.