-
Notifications
You must be signed in to change notification settings - Fork 404
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 assertions for each DOM query #548
Comments
I would be open to this. Sounds good. Hopefully someone can take it to contribute this new feature before I or other maintainer do it, which may take a while. |
I'm willing to make a contribution. My biggest concern is what I mentioned in the drawbacks section. These assertions would require a dependency on |
Hmmm, good point. @testing-library/core-maintainers thoughts? |
peer would be the proper way to handle this |
Agreed. That's the way my local test implementation works. We can assume most consumers of I can get started on a PR if everyone is OK with:
|
I'm ok with that. But let's give it a day or two, to see what others have to say. |
@benquarmby I say go for it. |
Made some progress here: main...benquarmby:jest-dom:jest-dom-query Please ignore the VSCode settings. They're just for my personal convenience and will be removed. Still a way to go before it's ready for review. |
Describe the feature you'd like:
TL;DR:
Please consider adding jest assertions for each of the eight query types provided by
@testing-library/dom
. Assertions based directly on DOM queries would make this advice much easier to follow.Problem Statement
In many tests, asserting on both the existence and absence of elements is key to proving correct behavior. While it's true that
getBy*
and friends already throw excellent diagnostic information when something can't be found:Fabricated Example
An app needs to show and hide a warning within a dialog as validity changes. This is what the test might look like today.
Drawbacks
Having assertions that are tightly integrated with DOM queries would require a dependency on
@testing-library/dom
which does not exist today. That said, the library is called@testing-library/jest-dom
, which implies the two are connected.Suggested implementation:
Proposal
Using "by role" as an example:
toContainOneByRole
which passes when there is precisely one element. The negated versionnot.toContainOneByRole
passes when there are zero or more than one elements. This has some similarities withgetByRole
.toContainAnyByRole
which passes when there are one or more elements. The negated versionnot.toContainAnyByRole
passes when there are zero elements. This has some similarities withgetAllByRole
.The assertion arguments would have exact symmetry with each query function. For example:
Repeat for the other seven query types in
@testing-library/dom
.Sample Implementation
A hastily put together implementation can be found in this gist: https://gist.github.com/benquarmby/7510252ab701669c2eaf3c0156dd680c. There are holes, but it works for demonstration purposes.
Describe alternatives you've considered:
@testing-library/jest-dom-query
or unofficial.Teachability, Documentation, Adoption, Migration Strategy:
Using the previous fabricated example, usage could look like this:
The text was updated successfully, but these errors were encountered: