@testing-library/cypress

Simple and complete custom Cypress commands and utilities that encourage good testing practices.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
@testing-library/cypress
1,7883810.0.17 months ago5 years agoMinified + gzip package size for @testing-library/cypress in KB

Readme

Cypress Testing Library

height="80" width="80" alt="tiger" src="https://raw.githubusercontent.com/testing-library/cypress-testing-library/main/other/tiger.png" />

Simple and complete custom Cypress commands and utilities that encourage good testing practices.


Read the docs | Edit the docs

!Build Statusbuild-badgebuild !Code Coveragecoverage-badgecoverage !versionversion-badgepackage !downloadsdownloads-badgenpmtrends !MIT Licenselicense-badgelicense !All Contributorsall-contributors-badge !PRs Welcomeprs-badgeprs !Code of Conductcoc-badgecoc

The problem

You want to use DOM Testing Librarydom-testing-library methods in your Cypresscypress tests.

This solution

This allows you to use all the useful DOM Testing Librarydom-testing-library methods in your tests.

Table of Contents


- With TypeScript - Intellisense for JavaScript with VS Code - Differences from DOM Testing Library - πŸ› Bugs - πŸ’‘ Feature Requests

Installation

This module is distributed via npmnpm which is bundled with nodenode 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:
{
  "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:
{
  "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:
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
You can find all Library definitions here.
To configure DOM Testing Library, use the following custom command:
cy.configureCypressTestingLibrary(config)

To show some simple examples (from cypress/e2e/find.cy.js):
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:
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.
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:
import {configure} from '@testing-library/cypress'
configure({testIdAttribute: 'data-test-id'})

It accepts all configurations listed in DOM testing library.

Other Solutions

I'm not aware of any, if you are please make a pull requestprs and add it here!

Issues

Looking to contribute? Look for the Good First Issuegood-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 Requestsrequests

Contributors ✨

Thanks goes to these people (emoji keyemojis):
<td align="center"><a href="https://kentcdodds.com"><img src="https://avatars.githubusercontent.com/u/1500684?v=3?s=100" width="100px;" alt=""/><br /><sub><b>Kent C. Dodds</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=kentcdodds" title="Code">πŸ’»</a> <a href="https://github.com/testing-library/cypress-testing-library/commits?author=kentcdodds" title="Documentation">πŸ“–</a> <a href="#infra-kentcdodds" title="Infrastructure (Hosting, Build-Tools, etc)">πŸš‡</a> <a href="https://github.com/testing-library/cypress-testing-library/commits?author=kentcdodds" title="Tests">⚠️</a></td>
<td align="center"><a href="https://sompylasar.github.io"><img src="https://avatars2.githubusercontent.com/u/498274?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ivan Babak</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=sompylasar" title="Code">πŸ’»</a> <a href="#ideas-sompylasar" title="Ideas, Planning, & Feedback">πŸ€”</a></td>
<td align="center"><a href="http://team.thebrain.pro"><img src="https://avatars1.githubusercontent.com/u/4002543?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Łukasz Gandecki</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=lgandecki" title="Code">πŸ’»</a> <a href="https://github.com/testing-library/cypress-testing-library/commits?author=lgandecki" title="Tests">⚠️</a></td>
<td align="center"><a href="https://github.com/npeterkamps"><img src="https://avatars1.githubusercontent.com/u/25429764?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Peter Kamps</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=npeterkamps" title="Code">πŸ’»</a> <a href="https://github.com/testing-library/cypress-testing-library/commits?author=npeterkamps" title="Documentation">πŸ“–</a> <a href="#ideas-npeterkamps" title="Ideas, Planning, & Feedback">πŸ€”</a> <a href="https://github.com/testing-library/cypress-testing-library/commits?author=npeterkamps" title="Tests">⚠️</a></td>
<td align="center"><a href="https://github.com/airato"><img src="https://avatars3.githubusercontent.com/u/4506749?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Airat Aminev</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=airato" title="Code">πŸ’»</a> <a href="https://github.com/testing-library/cypress-testing-library/commits?author=airato" title="Tests">⚠️</a> <a href="#tool-airato" title="Tools">πŸ”§</a></td>
<td align="center"><a href="https://www.webiny.com"><img src="https://avatars0.githubusercontent.com/u/5121148?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Adrian Smijulj</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=adrian1358" title="Code">πŸ’»</a></td>
<td align="center"><a href="https://www.ossfinder.com"><img src="https://avatars0.githubusercontent.com/u/12230408?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Soo Jae Hwang</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/issues?q=author%3Amisoguy" title="Bug reports">πŸ›</a> <a href="https://github.com/testing-library/cypress-testing-library/commits?author=misoguy" title="Code">πŸ’»</a> <a href="https://github.com/testing-library/cypress-testing-library/commits?author=misoguy" title="Tests">⚠️</a></td>
<td align="center"><a href="https://github.com/wKovacs64"><img src="https://avatars1.githubusercontent.com/u/1288694?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Justin Hall</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=wKovacs64" title="Code">πŸ’»</a> <a href="https://github.com/testing-library/cypress-testing-library/commits?author=wKovacs64" title="Tests">⚠️</a></td>
<td align="center"><a href="https://github.com/euZebe"><img src="https://avatars3.githubusercontent.com/u/9463809?v=4?s=100" width="100px;" alt=""/><br /><sub><b>euzebe</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=euZebe" title="Documentation">πŸ“–</a></td>
<td align="center"><a href="https://github.com/jkdowdle"><img src="https://avatars0.githubusercontent.com/u/19804196?v=4?s=100" width="100px;" alt=""/><br /><sub><b>jkdowdle</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=jkdowdle" title="Code">πŸ’»</a></td>
<td align="center"><a href="https://brian.ng"><img src="https://avatars3.githubusercontent.com/u/56288?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Brian Ng</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=existentialism" title="Code">πŸ’»</a></td>
<td align="center"><a href="https://karilaari.fi"><img src="https://avatars2.githubusercontent.com/u/2477131?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Kari Laari</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=klaari" title="Documentation">πŸ“–</a></td>
<td align="center"><a href="https://github.com/ppi-buck"><img src="https://avatars2.githubusercontent.com/u/37330764?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Basti Buck</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=ppi-buck" title="Code">πŸ’»</a></td>
<td align="center"><a href="https://github.com/ShimiTheFirst"><img src="https://avatars2.githubusercontent.com/u/25421369?v=4?s=100" width="100px;" alt=""/><br /><sub><b>ShimiTheFirst</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/issues?q=author%3AShimiTheFirst" title="Bug reports">πŸ›</a></td>
<td align="center"><a href="https://github.com/omerose"><img src="https://avatars2.githubusercontent.com/u/9358542?v=4?s=100" width="100px;" alt=""/><br /><sub><b>omerose</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=omerose" title="Documentation">πŸ“–</a></td>
<td align="center"><a href="http://www.aaronmcadam.com"><img src="https://avatars3.githubusercontent.com/u/37928?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Aaron Mc Adam</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=aaronmcadam" title="Code">πŸ’»</a> <a href="https://github.com/testing-library/cypress-testing-library/commits?author=aaronmcadam" title="Tests">⚠️</a></td>
<td align="center"><a href="https://twitter.com/karlhorky"><img src="https://avatars2.githubusercontent.com/u/1935696?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Karl Horky</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=karlhorky" title="Documentation">πŸ“–</a></td>
<td align="center"><a href="https://twitter.com/NoriSte"><img src="https://avatars0.githubusercontent.com/u/173663?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Stefano Magni</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=NoriSte" title="Code">πŸ’»</a> <a href="https://github.com/testing-library/cypress-testing-library/commits?author=NoriSte" title="Tests">⚠️</a> <a href="https://github.com/testing-library/cypress-testing-library/commits?author=NoriSte" title="Documentation">πŸ“–</a></td>
<td align="center"><a href="https://github.com/weyert"><img src="https://avatars3.githubusercontent.com/u/7049?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Weyert de Boer</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=weyert" title="Code">πŸ’»</a></td>
<td align="center"><a href="https://simjes.dev/"><img src="https://avatars0.githubusercontent.com/u/6494049?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Simon Jespersen</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=simjes" title="Code">πŸ’»</a> <a href="https://github.com/testing-library/cypress-testing-library/pulls?q=is%3Apr+reviewed-by%3Asimjes" title="Reviewed Pull Requests">πŸ‘€</a></td>
<td align="center"><a href="https://afontcu.dev"><img src="https://avatars0.githubusercontent.com/u/9197791?v=4?s=100" width="100px;" alt=""/><br /><sub><b>AdriΓ  Fontcuberta</b></sub></a><br /><a href="#infra-afontcu" title="Infrastructure (Hosting, Build-Tools, etc)">πŸš‡</a> <a href="https://github.com/testing-library/cypress-testing-library/commits?author=afontcu" title="Documentation">πŸ“–</a> <a href="https://github.com/testing-library/cypress-testing-library/pulls?q=is%3Apr+reviewed-by%3Aafontcu" title="Reviewed Pull Requests">πŸ‘€</a></td>
<td align="center"><a href="https://github.com/Megoos"><img src="https://avatars2.githubusercontent.com/u/9866017?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Mikhail Guskov</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/issues?q=author%3AMegoos" title="Bug reports">πŸ›</a></td>
<td align="center"><a href="https://jds.work"><img src="https://avatars1.githubusercontent.com/u/10285055?v=4?s=100" width="100px;" alt=""/><br /><sub><b>JD Gonzales</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=juliusdelta" title="Documentation">πŸ“–</a></td>
<td align="center"><a href="https://yvonnickfrin.dev"><img src="https://avatars0.githubusercontent.com/u/13099512?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Yvonnick FRIN</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=frinyvonnick" title="Documentation">πŸ“–</a></td>
<td align="center"><a href="https://www.franck-abgrall.me/"><img src="https://avatars3.githubusercontent.com/u/9840435?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Franck Abgrall</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/pulls?q=is%3Apr+reviewed-by%3Akefranabg" title="Reviewed Pull Requests">πŸ‘€</a></td>
<td align="center"><a href="http://twitter.com/tlrobinson"><img src="https://avatars0.githubusercontent.com/u/18193?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Tom Robinson</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=tlrobinson" title="Code">πŸ’»</a> <a href="https://github.com/testing-library/cypress-testing-library/commits?author=tlrobinson" title="Tests">⚠️</a></td>
<td align="center"><a href="https://github.com/NicholasBoll"><img src="https://avatars2.githubusercontent.com/u/338257?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Nicholas Boll</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=NicholasBoll" title="Code">πŸ’»</a> <a href="https://github.com/testing-library/cypress-testing-library/commits?author=NicholasBoll" title="Tests">⚠️</a></td>
<td align="center"><a href="https://github.com/FlopieUtd"><img src="https://avatars3.githubusercontent.com/u/23555863?v=4?s=100" width="100px;" alt=""/><br /><sub><b>FlopieUtd</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=FlopieUtd" title="Documentation">πŸ“–</a></td>
<td align="center"><a href="https://github.com/leosuncin"><img src="https://avatars1.githubusercontent.com/u/4307697?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jaime Leonardo Suncin Cruz</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/issues?q=author%3Aleosuncin" title="Bug reports">πŸ›</a> <a href="https://github.com/testing-library/cypress-testing-library/commits?author=leosuncin" title="Code">πŸ’»</a> <a href="https://github.com/testing-library/cypress-testing-library/commits?author=leosuncin" title="Tests">⚠️</a></td>
<td align="center"><a href="https://matt.travi.org"><img src="https://avatars1.githubusercontent.com/u/126441?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Matt Travi</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=travi" title="Code">πŸ’»</a></td>
<td align="center"><a href="https://michaeldeboey.be"><img src="https://avatars3.githubusercontent.com/u/6643991?v=4?s=100" width="100px;" alt=""/><br /><sub><b>MichaΓ«l De Boey</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=MichaelDeBoey" title="Code">πŸ’»</a></td>
<td align="center"><a href="https://huchen.dev"><img src="https://avatars3.githubusercontent.com/u/2078389?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Hu Chen</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=huchenme" title="Documentation">πŸ“–</a></td>
<td align="center"><a href="https://github.com/frederickfogerty"><img src="https://avatars0.githubusercontent.com/u/615334?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Frederick Fogerty</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=frederickfogerty" title="Code">πŸ’»</a></td>
<td align="center"><a href="https://kylemh.com"><img src="https://avatars1.githubusercontent.com/u/9523719?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Kyle Holmberg</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=kylemh" title="Documentation">πŸ“–</a></td>
<td align="center"><a href="https://github.com/Thisen"><img src="https://avatars2.githubusercontent.com/u/10807938?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Mathis MΓΈller</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=Thisen" title="Code">πŸ’»</a> <a href="https://github.com/testing-library/cypress-testing-library/commits?author=Thisen" title="Tests">⚠️</a></td>
<td align="center"><a href="https://github.com/SimenB"><img src="https://avatars1.githubusercontent.com/u/1404810?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Simen Bekkhus</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=SimenB" title="Code">πŸ’»</a></td>
<td align="center"><a href="https://github.com/amitmiran137"><img src="https://avatars.githubusercontent.com/u/47772523?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Amit Miran</b></sub></a><br /><a href="#infra-amitmiran137" title="Infrastructure (Hosting, Build-Tools, etc)">πŸš‡</a></td>
<td align="center"><a href="https://github.com/leschdom"><img src="https://avatars.githubusercontent.com/u/62334278?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Dominik Lesch</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=leschdom" title="Documentation">πŸ“–</a></td>
<td align="center"><a href="https://github.com/Andarist"><img src="https://avatars.githubusercontent.com/u/9800850?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Mateusz BurzyΕ„ski</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=Andarist" title="Code">πŸ’»</a></td>
<td align="center"><a href="https://nielsdb97.nl"><img src="https://avatars.githubusercontent.com/u/11021818?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Niels de Bruin</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=nielsdB97" title="Code">πŸ’»</a></td>
<td align="center"><a href="https://matan.io"><img src="https://avatars.githubusercontent.com/u/12711091?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Matan Borenkraout</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=MatanBobi" title="Code">πŸ’»</a></td>



This project follows the all-contributorsall-contributors specification. Contributions of any kind welcome!

LICENSE

MIT