Understand the differences between Cypress Testing Library and DOM Testing Library
mainWhile based on DOM Testing Library, Cypress Testing Library has several key differences to align with Cypress behavior:
- Element Types: It supports both jQuery elements and DOM nodes. When chaining a query, it extracts the first DOM node from the subject to use as the
containerfor the underlying DOM Testing Library function. - Query Support:
query*is not supported. Use.should('not.exist')to assert an element is missing.get*is not supported. Usefind*queries which use Cypress retryability.
- Selection Behavior:
find*commands will fail if more than one element is found (to maintain compatibility with other Testing Libraries).findAll*commands select multiple elements, behaving more like standard Cypress commands.
- Actions: Cypress handles actions on multiple elements by failing if more than one is found. For example,
cy.findAllByText('Some Text').click()will automatically fail if the selector matches multiple elements. - Enforcing Single Elements: To explicitly ensure only one element exists, use:
cy.findAllByText('Some Text').should('have.length', 1)cy.findByText('Some Text').should('exist')