You want to use DOM Testing Library
methods in your
Cypress tests.
This allows you to use all the useful
DOM Testing Library
methods in your tests.
This module is distributed via npm which is bundled with node and
should be installed as one of your project's devDependencies
:
npm install --save-dev @testing-library/cypress
Typings are defined in @types/testing-library__cypress
at DefinitelyTyped,
and should be added as follows in tsconfig.json
:
{
"compilerOptions": {
"types": ["cypress", "@types/testing-library__cypress"]
}
}
Cypress Testing Library
extends Cypress' cy
command.
Add this line to your project's cypress/support/commands.js
:
import '@testing-library/cypress/add-commands'
You can now use all of DOM Testing Library
's findBy
, findAllBy
, queryBy
and queryAllBy
commands.
See the DOM Testing Library
docs for reference
You can find all Library definitions here.
To show some simple examples (from cypress/integration/query.spec.js or cypress/integration/find.spec.js):
cy.queryByText('Button Text').should('exist')
cy.queryByText('Non-existing Button Text').should('not.exist')
cy.queryByLabelText('Label text', {timeout: 7000}).should('exist')
cy.findAllByText('Jackie Chan').click({multiple: true})
cy.get('form').within(() => {
cy.findByText('Button Text').should('exist')
})
cy.get('form').then(subject => {
cy.findByText('Button Text', {container: subject}).should('exist')
})
Cypress Testing Library
supports both jQuery elements and DOM nodes. This is
necessary because Cypress uses jQuery elements, while DOM Testing Library
expects DOM nodes. When you pass a jQuery element as container
, it will get
the first DOM node from the collection and use that as the container
parameter
for the DOM Testing Library
functions.
I'm not aware of any, if you are please make a pull request and add it here!
Thanks goes to these people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
MIT