Simple and complete custom Cypress commands and utilities that encourage good
testing practices.
[**Read the docs**](https://testing-library.com/cypress) |
[Edit the docs](https://github.com/alexkrolick/testing-library-docs)
[![Build Status][build-badge]][build]
[![Code Coverage][coverage-badge]][coverage]
[![version][version-badge]][package] [![downloads][downloads-badge]][npmtrends]
[![MIT License][license-badge]][license]
[![All Contributors](https://img.shields.io/badge/all_contributors-25-orange.svg?style=flat-square)](#contributors)
[![PRs Welcome][prs-badge]][prs] [![Code of Conduct][coc-badge]][coc]
[![Watch on GitHub][github-watch-badge]][github-watch]
[![Star on GitHub][github-star-badge]][github-star]
[![Tweet][twitter-badge]][twitter]
## The problem
You want to use [`DOM Testing Library`][dom-testing-library] methods in your
[Cypress][cypress] tests.
## This solution
This allows you to use all the useful
[`DOM Testing Library`][dom-testing-library] methods in your tests.
## Table of Contents
- [Installation](#installation)
- [With TypeScript](#with-typescript)
- [Usage](#usage)
- [Differences from DOM Testing Library](#differences-from-dom-testing-library)
- [Other Solutions](#other-solutions)
- [Contributors](#contributors)
- [LICENSE](#license)
## Installation
This module is distributed via [npm][npm] which is bundled with [node][node] and
should be installed as one of your project's `devDependencies`:
```
npm install --save-dev @testing-library/cypress
```
### With TypeScript
Typings are defined in `@types/testing-library__cypress` at
[DefinitelyTyped](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/testing-library__cypress),
and should be added as follows in `tsconfig.json`:
```json
{
"compilerOptions": {
"types": ["cypress", "@types/testing-library__cypress"]
}
}
```
## Usage
`Cypress Testing Library` extends Cypress' `cy` command.
Add this line to your project's `cypress/support/commands.js`:
```javascript
import '@testing-library/cypress/add-commands'
```
You can now use all of `DOM Testing Library`'s `findBy` and `findAllBy`
commands.
[See the `DOM Testing Library` docs for reference](https://testing-library.com)
You can find
[all Library definitions here](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/testing-library__cypress/index.d.ts).
To configure DOM Testing Library, use the following custom command:
```javascript
cy.configureCypressTestingLibrary(config)
```
To show some simple examples (from
[cypress/integration/find.spec.js](cypress/integration/find.spec.js)):
```javascript
cy.findAllByText('Button Text').should('exist')
cy.findAllByText('Non-existing Button Text').should('not.exist')
cy.findAllByLabelText('Label text', {timeout: 7000}).should('exist')
cy.findAllByText('Jackie Chan').click()
// findAllByText _inside_ a form element
cy.get('form')
.findAllByText('Button Text')
.should('exist')
```
### 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.
`get*` and `query*` queries are disabled. `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
Libraries.
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
`findAllByText`:
```javascript
cy.findAllByText('Some Text').click()
```
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.
```javascript
cy.findAllByText('Some Text').should('have.length', 1)
cy.findByText('Some Text').should('exist')
```
## Config testIdAttribute
If you would like to change the default testId from `data-testId` to `data-test-id`, add to your project's `cypress/support/index.js`:
```javascript
import {configure} from '@testing-library/cypress'
configure({testIdAttribute: 'data-test-id'})
```
It accepts all configurations listed in [DOM testing library](https://testing-library.com/docs/dom-testing-library/api-configuration).
## Other Solutions
I'm not aware of any, if you are please [make a pull request][prs] and add it
here!
## Contributors
Thanks goes to these people ([emoji key][emojis]):