-
Notifications
You must be signed in to change notification settings - Fork 410
Open
Labels
needs discussionWe need to discuss this to come up with a good solutionWe need to discuss this to come up with a good solution
Description
Describe the feature:
Would it make sense to extend the current matchers to handle NodeLists? Is there a need for this? I was thinking about queryAllBy*
and querySelectorAll
and wondering if it would be helpful to pass them directly to the matchers.
For example:
expect(queryAllByText('text')).toBeVisible();
expect(document.querySelectorAll('button')).toBeDisabled();
If all elements match the matcher, then it passes. If any of the elements do not match then it fails.
Metadata
Metadata
Assignees
Labels
needs discussionWe need to discuss this to come up with a good solutionWe need to discuss this to come up with a good solution
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
kentcdodds commentedon Aug 18, 2018
I'm in favor
gnapse commentedon Aug 18, 2018
Sounds good. I'm in favor too.
I've got a question though: what would happen if the list of elements is empty?
The regular rules of logic would dictate that in that case an assertion passes. It gets even more interesting when you consider the negative case too, which should also pass, according to a strict logic:
However, I'm not sure that's what should happen. Perhaps these should all throw an exception when they get an empty collection of elements?
kentcdodds commentedon Aug 20, 2018
Thinking about what people are probably trying to do, I think that an empty list should throw an error in either case. If they want to verify the list is empty then they can add an assertion for that specifically. I think doing anything else could lead to passing tests but a broken app.
gnapse commentedon Aug 20, 2018
Exactly what I was thinking.
smacpherson64 commentedon Aug 21, 2018
Sounds good! I will aim to take a stab at implementing the idea this week!
smacpherson64 commentedon Aug 24, 2018
@gnapse and @kentcdodds, I have an HOF concept but want to make sure it is on the right track.
My thought is the HOF wrapping each matcher in index.js would allow us to not have to adjust any of the matchers directly. If the HOF determines that it is not a nodelist it just runs the normal matcher.
https://github.com/smacpherson64/jest-dom/commit/17770e25c5c0a4fece84bc4b7bcfbcf4c9d0fe4d
There is a lot that needs to be cleaned up. Feedback, negative or positive, is appreciated! Thanks for letting me be part!
gnapse commentedon Aug 24, 2018
On a second thought, I'm a bit worried about the complexity this feature introduces in the code, compared to the return. In part I'm thinking of this now, after seeing the code, and realizing that this will make error messages more complex and difficult to be clear enough.
What are you planning to show, for instance, upon failure, as expected and received in the jest error message? If 5 out of 9 elements in a node list fail to pass the expectation, what would be a sensible way to convey all that information? And what if the user, by using an incorrect selector, accidentally matches dozens of elements, a significant part of which pass an expectation and others do not.
smacpherson64 commentedon Aug 24, 2018
I do agree, it adds a bit of complexity.
When it comes to the error messages I think we have a few options to aim for clarity:
Option 1 - The First Error Only
Option 2 - # out of total
Option 3 - Display all failing elements and their indexes
Option 4 - All of the Above
Option 5 - All of the Above, Sampled (for large sets)
Option 6 - Element Path Example
smacpherson64 commentedon Aug 29, 2018
@gnapse, For this item, I am planning to get it into a working state and so we can see examples of real messages. I think the real messages will help see if the examples make sense or if they are too complicated / convoluted.
gnapse commentedon Aug 29, 2018
Sounds like a plan. I do want to see it first in action. Cause I'm not yet convinced this will be automatically useful without user-friendly messaging.
smacpherson64 commentedon Sep 3, 2018
@gnapse, I have a basic implementation ready, I am putting it as a PR. It is highly likely we will have some tweaks, I just wanted to get a working concept your way for testing purposes and reviewing the code.
smacpherson64 commentedon Sep 3, 2018
Below is an example of 100 elements failing the toBeEmpty matcher:
smacpherson64 commentedon Feb 6, 2019
Going to close this for now!
benmonro commentedon Dec 4, 2019
what ever happened to this? I would love to see it revived
gnapse commentedon Dec 5, 2019
@benmonro in that case I'd like you to chime in on the concerns raised above.
benmonro commentedon Dec 5, 2019
Well My thought is to keep it simple and use @smacpherson64 's option 1. The reason for this is that currently the way to do this is to iterate on the list and expect on each element so the behavior would be the same as that.
gnapse commentedon Dec 5, 2019
Ok, fair enough. However, on re-reading the options, I would go for option 2. Is the same as option 1 but it gives a bit more info about the overall state of affairs.