Skip to content

πŸ… Simple and complete custom Cypress commands and utilities that encourage good testing practices.

License

Notifications You must be signed in to change notification settings

testing-library/cypress-testing-library

Repository files navigation

cypress-testing-library

tiger

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

Read the docs | Edit the docs


Build Status Code Coverage version downloads MIT License

All Contributors PRs Welcome Code of Conduct

Watch on GitHub Star on GitHub Tweet

The problem

You want to use dom-testing-library methods in your Cypress tests.

This solution

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

Table of Contents

Installation

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

With TypeScript

Typings are defined under @testing-library/cypress/typings, and should be added as follows in tsconfig.json:

{
  "compilerOptions": {
    "types": ["cypress", "../@testing-library/cypress/typings"]
  }
}

Or if tsconfig.json has a baseUrl of ../node_modules as recommended in the official Cypress documentation use:

{
  "compilerOptions": {
    "types": ["cypress", "@testing-library/cypress/typings"]
  }
}

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 getBy, getAllBy, 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/commands.spec.js):

cy.getAllByText('Jackie Chan').click()
cy.queryByText('Button Text').should('exist')
cy.queryByText('Non-existing Button Text').should('not.exist')
cy.queryByLabelText('Label text', {timeout: 7000}).should('exist')
cy.get('form').within(() => {
  cy.getByText('Button Text').should('exist')
})
cy.get('form').then(subject => {
  cy.getByText('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.

Other Solutions

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

Contributors

Thanks goes to these people (emoji key):

Kent C. Dodds
Kent C. Dodds

πŸ’» πŸ“– πŸš‡ ⚠️
Ivan Babak
Ivan Babak

πŸ’» πŸ€”
Łukasz Gandecki
Łukasz Gandecki

πŸ’» ⚠️
Peter Kamps
Peter Kamps

πŸ’» πŸ“– πŸ€” ⚠️
Airat Aminev
Airat Aminev

πŸ’» ⚠️ πŸ”§
Adrian Smijulj
Adrian Smijulj

πŸ’»
Soo Jae Hwang
Soo Jae Hwang

πŸ› πŸ’» ⚠️
Justin Hall
Justin Hall

πŸ’» ⚠️
euzebe
euzebe

πŸ“–
jkdowdle
jkdowdle

πŸ’»
Brian Ng
Brian Ng

πŸ’»
Kari Laari
Kari Laari

πŸ“–
Basti Buck
Basti Buck

πŸ’»
ShimiTheFirst
ShimiTheFirst

πŸ›
omerose
omerose

πŸ“–
Aaron Mc Adam
Aaron Mc Adam

πŸ’» ⚠️
Karl Horky
Karl Horky

πŸ“–
Stefano Magni
Stefano Magni

πŸ’» ⚠️ πŸ“–
Weyert de Boer
Weyert de Boer

πŸ’»

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

LICENSE

MIT