Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/testing-library/cypress-testing-library
π
Simple and complete custom Cypress commands and utilities that encourage good testing practices.
https://github.com/testing-library/cypress-testing-library
testing
Last synced: 3 days ago
JSON representation
π Simple and complete custom Cypress commands and utilities that encourage good testing practices.
- Host: GitHub
- URL: https://github.com/testing-library/cypress-testing-library
- Owner: testing-library
- License: mit
- Created: 2018-04-06T14:31:43.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2024-05-31T12:53:54.000Z (7 months ago)
- Last Synced: 2024-12-01T19:26:25.393Z (11 days ago)
- Topics: testing
- Language: JavaScript
- Homepage: http://npm.im/@testing-library/cypress
- Size: 660 KB
- Stars: 1,804
- Watchers: 29
- Forks: 153
- Open Issues: 40
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
- stars - cypress-testing-library - library | 1804 | (JavaScript)
- stars - cypress-testing-library - library | 1804 | (JavaScript)
README
Cypress Testing Library
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][all-contributors-badge]](#contributors-)
[![PRs Welcome][prs-badge]][prs]
[![Code of Conduct][coc-badge]][coc]## 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)
- [Intellisense for JavaScript with VS Code](#intellisense-for-javascript-with-vs-code)
- [Usage](#usage)
- [Differences from DOM Testing Library](#differences-from-dom-testing-library)
- [Config testIdAttribute](#config-testidattribute)
- [Other Solutions](#other-solutions)
- [Issues](#issues)
- [π Bugs](#-bugs)
- [π‘ Feature Requests](#-feature-requests)
- [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 should be added as follows in `tsconfig.json`:
```json
{
"compilerOptions": {
"types": ["cypress", "@testing-library/cypress"]
}
}
```### Intellisense for JavaScript with VS Code
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 `jsconfig.json` file:```json
{
"include": ["node_modules/cypress", "./cypress/**/*.js"]
}
```## 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/testing-library/cypress-testing-library/tree/main/types/index.d.ts).To configure DOM Testing Library, use the following custom command:
```javascript
cy.configureCypressTestingLibrary(config)
```To show some simple examples (from
[cypress/e2e/find.cy.js](cypress/e2e/find.cy.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.`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
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!## Issues
_Looking to contribute? Look for the [Good First Issue][good-first-issue]
label._### π Bugs
Please file an issue for bugs, missing documentation, or unexpected behavior.
[**See Bugs**][bugs]
### π‘ 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.[**See Feature Requests**][requests]
## Contributors β¨
Thanks goes to these people ([emoji key][emojis]):
Kent C. Dodds
π» π π β οΈ
Ivan Babak
π» π€
Εukasz Gandecki
π» β οΈ
Peter Kamps
π» π π€ β οΈ
Airat Aminev
π» β οΈ π§
Adrian Smijulj
π»
Soo Jae Hwang
π π» β οΈ
Justin Hall
π» β οΈ
euzebe
π
jkdowdle
π»
Brian Ng
π»
Kari Laari
π
Basti Buck
π»
ShimiTheFirst
π
omerose
π
Aaron Mc Adam
π» β οΈ
Karl Horky
π
Stefano Magni
π» β οΈ π
Weyert de Boer
π»
Simon Jespersen
π» π
AdriΓ Fontcuberta
π π π
Mikhail Guskov
π
JD Gonzales
π
Yvonnick FRIN
π
Franck Abgrall
π
Tom Robinson
π» β οΈ
Nicholas Boll
π» β οΈ
FlopieUtd
π
Jaime Leonardo Suncin Cruz
π π» β οΈ
Matt Travi
π»
MichaΓ«l De Boey
π»
Hu Chen
π
Frederick Fogerty
π»
Kyle Holmberg
π
Mathis MΓΈller
π» β οΈ
Simen Bekkhus
π»
Amit Miran
π
Dominik Lesch
π
Mateusz BurzyΕski
π»
Niels de Bruin
π»
Matan Borenkraout
π»
This project follows the [all-contributors][all-contributors] specification.
Contributions of any kind welcome!## LICENSE
MIT
[npm]: https://npmjs.com
[node]: https://nodejs.org
[build-badge]: https://img.shields.io/github/workflow/status/testing-library/cypress-testing-library/validate?logo=github&style=flat-square
[build]: https://github.com/testing-library/cypress-testing-library/actions?query=workflow%3Avalidate
[coverage-badge]: https://img.shields.io/codecov/c/github/testing-library/cypress-testing-library.svg?style=flat-square
[coverage]: https://codecov.io/github/testing-library/cypress-testing-library
[version-badge]: https://img.shields.io/npm/v/@testing-library/cypress.svg?style=flat-square
[package]: https://www.npmjs.com/package/@testing-library/cypress
[downloads-badge]: https://img.shields.io/npm/dm/@testing-library/cypress.svg?style=flat-square
[npmtrends]: http://www.npmtrends.com/@testing-library/cypress
[license-badge]: https://img.shields.io/npm/l/@testing-library/cypress.svg?style=flat-square
[license]: https://github.com/testing-library/cypress-testing-library/blob/main/LICENSE
[prs-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square
[prs]: http://makeapullrequest.com
[coc-badge]: https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square
[coc]: https://github.com/testing-library/cypress-testing-library/blob/main/other/CODE_OF_CONDUCT.md
[emojis]: https://allcontributors.org/docs/en/emoji-key
[all-contributors]: https://github.com/all-contributors/all-contributors
[all-contributors-badge]: https://img.shields.io/github/all-contributors/testing-library/cypress-testing-library?color=orange&style=flat-square
[bugs]: https://github.com/testing-library/cypress-testing-library/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+label%3A%22%F0%9F%90%9B+Bug%22+sort%3Acreated-desc
[requests]: https://github.com/testing-library/cypress-testing-library/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc+label%3Aenhancement
[good-first-issue]: https://github.com/testing-library/cypress-testing-library/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc+label%3A%22good+first+issue%22[cypress]: https://cypress.io
[dom-testing-library]: https://github.com/testing-library/dom-testing-library