Cypress Testing Library
Simple and complete custom Cypress commands and utilities that encourage good testing practices.
This allows you to use all the useful
DOM Testing Library methods in your tests.
Table of Contents
- Config testIdAttribute
- Other Solutions
- Contributors ✨
npm install --save-dev @testing-library/cypress
Typings should be added as follows in
If you're not using TypeScript, you use VS Code, and want to have
code-completion with the methods from this library, simply add the following
line to your project's root-level
Cypress Testing Library extends Cypress'
Add this line to your project's
You can now use all of
DOM Testing Library's
DOM Testing Library docs for reference
You can find all Library definitions here.
To configure DOM Testing Library, use the following custom command:
To show some simple examples (from cypress/integration/find.spec.js):
cycycycy// findAllByText _inside_ a form elementcy
Differences from DOM Testing Library
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 chain a query, it will get the first DOM node from
subject of the collection and use that as the
container parameter for the
DOM Testing Library functions.
query* queries are not supported. You should use the `should('not.exist')
assertion instead to check for the absence of an element.
get* queries are not supported.
find* queries do not use the Promise API of
DOM Testing Library, but instead forward to the
get* queries and use
Cypress' built-in retryability using error messages from
get* APIs to forward
as error messages if a query fails.
findAll* can select more than one element and is closer in functionality to
how Cypress built-in commands work.
find* commands will fail if more than one
element is found that matches the criteria which is not how built-in Cypress
commands work, but is provided for closer compatibility to other Testing
Cypress handles actions when there is only one element found. For example, the
following will work without having to limit to only 1 returned element. The
cy.click will automatically fail if more than 1 element is returned by the
If you intend to enforce only 1 element is returned by a selector, the following examples will both fail if more than one element is found.
If you would like to change the default testId from
data-test-id, add to your project's
It accepts all configurations listed in DOM testing library.
I'm not aware of any, if you are please make a pull request and add it here!
Looking to contribute? Look for the Good First Issue label.
Please file an issue for bugs, missing documentation, or unexpected behavior.
💡 Feature Requests
Please file an issue to suggest new features. Vote on feature requests by adding a 👍. This helps maintainers prioritize what to work on.
Thanks goes to these people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!