-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add toHaveAttribute custom matcher (closes #43) #44
Add toHaveAttribute custom matcher (closes #43) #44
Conversation
printReceived, | ||
printExpected, | ||
stringify, | ||
RECEIVED_COLOR as receivedColor, |
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.
Just wondering here, can't we use printReceived
? Is that RECEIVED_COLOR
does something apart from printReceived
?
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. printReceived
uses RECEIVED_COLOR
internally, but it also stringifies the argument. But in the new code I added I wanted to show attribute-name="value"
all in the received or expected color, without stringifying all of it. If I used printReceived
/printExpected
I'd have the whole attribute-name="value"
itself surrounded by double quotes (and possibly those inner quotes escaped then).
src/jest-extensions.js
Outdated
expectedValue, | ||
receivedValue, | ||
) | ||
const matcher = matcherHint( |
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 have used assertMessage
method to handle the messages. Can't we re-use them? Like we have done it over here: https://github.com/gnapse/react-testing-library/blob/bfeb26aa29e2b36cbf254a6cb8dc6934e32a055e/src/jest-extensions.js#L89
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.
@antoaravinth Not in this case. The messages I wanted to generate do not comply with the predefined format that assertMessage
uses.
BTW, not even the two already existing custom matchers used this function. It's only being used in one of them. I do not think we can expect all or even most matchers to always adhere to this expected/received constrained message format. I was checking at jest's own codebase and how they approach generating messages, and the most common functionality they have across all them is encoded in all the functions they also publish in jest-matcher-utils
and that we're using 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.
Yes, I got it. Just wanted to check. Cool, thanks!
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.
That being said, although I do not object myself to how the code ended up in this function for generating the message, I get it if it looks too complex, and I'm of course open to any suggestions to improve it or even the resulting messages as well. I would not like though to do so by sacrificing too much of the friendliness of the resulting messages.
This matcher, although simple in the actual condition being tested, covers a few cases (e.g. it can test if the attribute is merely present or not, or it can test also that it has a specific value). Therefore the messages for the positive and negative cases, each of these handling the two cases of the attr value being tested or not, yields four different kind of resulting messages.
@gnapse This is great! Left few comments! |
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.
Looking good! Could you take a screenshot of what a failure looks like for both the .toHaveAttribute
and .not.toHaveAttribute
cases?
Sure, here it is, the four different possible cases: // Expect attribute to be present and with a certain value
.toHaveAttribute(attr, value)
.not.toHaveAttribute(attr, value)
// Expect attribute to be present, regardless of its value
.toHaveAttribute(attr)
.not.toHaveAttribute(attr) (The lines with the bullet points in front of them are the labels given to the |
Awesome. Could you update it so it resembles the errors from other assertions? Like this: Specific differences are:
Let me know if you have questions :) |
@kentcdodds I did points 2 and 3 above. However, regarding the first point, that is apparently a relatively new feature of the jest helper However, it was odd that I did not get that comment when using Do you prefer if I add the comment myself using |
Let's use the |
Me neither! I learned it while working on this. That's a huge part of what I love about contributing to open source. |
? `element.hasAttribute(${stringify(name)})` | ||
: `element.getAttribute(${stringify(name)}) === ${stringify(value)}` | ||
} | ||
|
||
const extensions = { | ||
toBeInTheDOM(received) { | ||
getDisplayName(received) |
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 just noticed that this actually isn't doing anything. I'm pretty sure we should assign it a value called displayName
and use that somehow or remove it entirely... Probably good for another PR 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.
Yes, I noticed that too, but I preferred no to touch it here as it's unrelated.
I do have a few code cleanup proposals for this file, that I was meaning to provide in a separate PR. For instance, the pattern of returning something different on the matchers depending on wether they pass
ed or not seems redundant to me.
Also it'd be good to bring the other two matchers messages up to the standards that you have proposed for .toHaveAttribute
.
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, I'd love it if you could clean this file up a bit in another PR! Let's just see what we need to do to increase coverage back to 100%.
Open coverage/lcov-report/index.html
in your browser to see what's left 👍
Codecov Report
@@ Coverage Diff @@
## master #44 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 5 5
Lines 85 98 +13
Branches 19 22 +3
=====================================
+ Hits 85 98 +13
Continue to review full report at Codecov.
|
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 excellent!
…elease-15.10.6 Update semantic-release to the latest version 🚀
Feedback is welcome on the code to build the message for the custom matcher. It got a bit tricky but I think it's worth it to give human-friendly messages of what went wrong, and there are quite a few cases to cover there.
Closes #43