-
Notifications
You must be signed in to change notification settings - Fork 153
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
Queries match elements inside the <head /> part of the document #134
Comments
Hi @gnapse As far as I know, to only find the h1 in the body, you could do something like: However, this kinda becomes testing implementation details because if you were to change the Maybe @kentcdodds has a better suggestion 😅... Thanks, |
Thanks @pranjaljately! This would be best: cy.findByRole('heading', {name: /hello world/i }).should('exist') That would work regardless of the level of heading. It's also more semantically correct 🤘 |
As for the assertion that queries should be scoped to the body, I'm curious how Cypress does this... I'm pretty sure they scope to the whole document. |
Awesome! Just learnt something new 😁 Thanks, @kentcdodds |
Indeed, the However, I'd still argue the issue remains, as the other queries are still valid queries, and a |
Well... Maybe we should do this then: testing-library/dom-testing-library#593 |
cypress-testing-library
version: 5.3.0Relevant code or config
What you did
Find an element by its text content.
What happened
Since the document title element in the head area also matches, the query tells me that it found more than one element with the expected text content.
Problem description
The queries are using the entire document as the container.
Suggested solution
Maybe the queries should use the body as the container. Elements in the head area cannot be interacted with and are generally not visible to the user. Except maybe for the title, but there should be more declarative ways to check for it than
findByText
.The text was updated successfully, but these errors were encountered: