Closed tyczynski closed 1 year ago
This is not a supported use-case. The queries in this plugin are like the cy.find
command from Cypress. We chose find*
instead of get*
to both align with the async nature of other Testing Library libraries like React Testing Library and to align with the previous subject of the cy.find
command. This allows the following to work:
cy.findByRole('dialog')
.findByRole('button') // button within the dialog
For example, the following will fail with the same message you have:
describe('Homepage', () => {
it('should contain "Hello World!" title', async () => {
cy.visit('/')
.find('anySelector')
.should('exist');
});
});
cy.visit
yields the window object which is not a valid element. The error message comes from Cypress based on the previous subject requirements of our commands.
Your example must be rewritten as:
describe('Homepage', () => {
it('should contain "Hello World!" title', async () => {
cy.visit('/')
cy.findByText(/hello world!/i)
.should('exist');
});
});
Think of Cypress commands as a sentence. Each command contains a subject. Within a sentence, the sentence can continue as long as the subject is narrowed. If the subject changes, it is a new sentence.
cy.findByRole('dialog')
.findByRole('button') // only buttons within a dialog rather than all buttons on the page
.click()
cypress-testing-library
version: 8.0.2node
version: 16.12.0npm
version: 8.1.0What you did:
Test
What happened:
Reproduction repository:
https://github.com/tyczynski/nextjs-boilerplate/tree/ctl-error
Problem description:
I wrote a simple test that should find the "Hello World!" heading. I used
.findByText(/hello world!/i)
, but this query works when test is asynchronous:Suggested solution: The test should not be asynchronous to work according to the documentation and examples. https://github.com/testing-library/cypress-testing-library/blob/97939da7d4707a71049884c0324c0eda56e26fc2/cypress/integration/find.spec.js