Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upDiscussion - extending matchers to handle NodeList #52
Comments
|
I'm in favor |
|
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: // allButtons is an empty list in a page without buttons
const allButtons = document.querySelectorAll('button')
expect(allButtons).toBeDisabled(); // Should this pass?
expect(allButtons).not.toBeDisabled(); // And this?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? |
|
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. |
|
Exactly what I was thinking. |
|
Sounds good! I will aim to take a stab at implementing the idea this week! |
|
@gnapse and @kentcdodds, I have an HOF concept but want to make sure it is on the right track. 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! |
|
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. |
|
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
|
|
@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. |
|
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. |
|
@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. |
|
Below is an example of 100 elements failing the toBeEmpty matcher:
|
|
Going to close this for now! |
|
what ever happened to this? I would love to see it revived |
|
@benmonro in that case I'd like you to chime in on the concerns raised above. |
|
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. |
|
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. |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

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*andquerySelectorAlland wondering if it would be helpful to pass them directly to the matchers.For example:
If all elements match the matcher, then it passes. If any of the elements do not match then it fails.