-
-
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
Support AssertionError expected and actual values #3217
Conversation
this.addExpectationResult( | ||
false, | ||
{ | ||
matcherName: '', | ||
matcherName: 'blah blah', |
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.
If I leave matcherName
as an empty string then what I get is the same thing as if I had not made any changes at all. I'm unsure of what this should be set to though.
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.
Note that when the matcherName
is blah blah
it does not fail any tests either 🤔
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.
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.
interesting. I think it's changing the way we deal with other types of errors too. Let me check out the branch and see what's going on
this.addExpectationResult( | ||
false, | ||
{ | ||
matcherName: '', | ||
matcherName: 'blah blah', | ||
message, |
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.
I've verified that adding this property (even when it's undefined
in most cases) does not cause a negative impact on the other tests. Though the tests are failing due to snapshot issues noted in #3216
`Received:\n` + | ||
` ${printReceived(actual)}` + | ||
(diffString ? `\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.
Any way we can make the call to diff
and the message lazy, as in message = () => {…}
? Otherwise I'm a bit worried here about perf in the case where we don't actually need the message.
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.
I tried that earlier and the process just hung forever which I thought was odd...
@@ -121,10 +127,26 @@ Spec.prototype.onException = function onException(e) { | |||
return; | |||
} | |||
|
|||
const {expected, actual} = e; |
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.
sometimes people do strange things like
throw null;
i wonder if Jest will blow up here
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.
Ah, yeah, that's totally weird, and it probably would blow up. We can add || {}
to that.
this.addExpectationResult( | ||
false, | ||
{ | ||
matcherName: '', | ||
matcherName: 'blah blah', |
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.
interesting. I think it's changing the way we deal with other types of errors too. Let me check out the branch and see what's going on
hey @kentcdodds |
I rebased @DmitriiAbramov's commit from #3225 because I figure that his change will be merged first and it's probably useful to see how this will integrate with it. |
● assert.deepEqual | ||
|
||
Error | ||
expect(received).toBe(expected) |
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.
Looks like toBe
is probably the wrong thing to use for the matcher hint... 🤔
To be clear, I'm looking for some direction on this pull request. Any help appreciated :) |
@kentcdodds i was thinking that we should probably pull this functionality out into a separate module that's responsible for node assert errors only. const assertionErrorMessage = formatNodeAssertionError(error);
if (assertionErrorMessage) {
return this.addExpectationResult(
false,
{
matcherName: SOME_CONSTANT_THAT_HAS_AN_ASSERTION_MATCHER_VALUE,
message: assertionErrorMessage,
passed: false,
// ...
},
);
} else {
// do wathever the default jasmine behavior is
} this way whenever we kill jasmine (it is happening) we can still reuse the logic and it'll be fairly independent. further on the assertion formatting, i think we can create a completely separate set of messages for each of the assertion failures. i tried to throw a few errors in the console using this: try { assert.deepEqual(false, 1) } catch (error) { console.log(error) } and i found that you can differentiate them using the so i think we can format messages like:
what do you think? |
That sounds great. Any suggestions on the coloring issue? |
hm. it looks like the entire message is dimmed. I'm not sure, but i think the reset ASCII code should fix it. ( |
@kentcdodds just a heads-up – I'm working on refining this PR so that errors have proper stack traces and indenting. Will update it tomorrow or so. Here's a preview: |
Oh awesome, thank you @thymikee! |
I've taken all possible functions from Assert docs. Since there's no way to differentiate form This is how I print custom message assertion message: And more complex matchers: They now look pretty similar to our Not so happy with |
I think this is great! Thank you so much @thymikee! What can I do to help get it across the finish line?! |
Got support for throwing matchers: @kentcdodds thanks! I think this is ready for a review. |
Codecov Report
@@ Coverage Diff @@
## master #3217 +/- ##
=========================================
- Coverage 64.24% 63.9% -0.35%
=========================================
Files 175 176 +1
Lines 6436 6471 +35
Branches 4 4
=========================================
Hits 4135 4135
- Misses 2300 2335 +35
Partials 1 1
Continue to review full report at Codecov.
|
|
||
assert.deepEqual(received, expected) | ||
|
||
Expected value to deepEqual to: |
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.
Is this just like ==, but deep? In that case, how about:
"Expected value to deeply equal:"
maybe?
|
||
assert.notDeepEqual(received, expected) | ||
|
||
Expected value to notDeepEqual to: |
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.
"not to deeply equal"?
|
||
assert.deepStrictEqual(received, expected) | ||
|
||
Expected value to deepStrictEqual to: |
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.
"to deeply and strictly equal"?
But it didn't throw anything. | ||
|
||
Message: | ||
Missing expected exception.. |
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 coming from assert, right?
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.
Yes, assert
generates this message and marks that it's not generated 🤷♂️, that's why it's shown here.
return; | ||
} | ||
|
||
if (error instanceof require('assert').AssertionError) { | ||
const assertionErrorMessage = require('jest-matchers/build/assert-support'); |
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 shouldn't do this. Can we move assert-support out of jest-matchers? Can it be in jest-jasmine2 instead?
This is nice! I would recommend not to put it into jest-matchers, though. It's a jest feature, not a jest-matchers feature (to be renamed to 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.
See comments.
Updated the messaging and moved |
Let's do it. |
* node `assert` snapshot tests * WIP: getting close to a working version * Refactor assert error messages * Remove jest-diff from jest-jasmine2 deps * Support throws and doesNotThrow assert matchers * Update snapshots * Fix snapshots across different node verions * Human readable messages
* node `assert` snapshot tests * WIP: getting close to a working version * Refactor assert error messages * Remove jest-diff from jest-jasmine2 deps * Support throws and doesNotThrow assert matchers * Update snapshots * Fix snapshots across different node verions * Human readable messages
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
Closes #3173 "Support assert module more cleanly" - basically include
error.expected
anderror.actual
in the error message.Test plan
I've got a project that's using ESLint's tester (which uses the
assert.equal
method). Here's a before:And here's after:
As you can see, I could use some color fixes. I'll also note a few places in the code I'm unsure of and could use some help with.