Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

testing-library/cypress-testing-library

tiger

Simple and complete custom Cypress commands and utilities that encourage goodtesting practices.

Read the docs |Edit the docs


Build StatusCode CoverageversiondownloadsMIT LicenseAll ContributorsPRs WelcomeCode of Conduct

TestingJavaScript.com Learn the smart, efficient way to test any JavaScript application.

The problem

You want to useDOM Testing Library methods in yourCypress tests.

This solution

This allows you to use all the usefulDOM Testing Library methods in your tests.

Table of Contents

Installation

This module is distributed vianpm which is bundled withnode andshould be installed as one of your project'sdevDependencies:

npm install --save-dev @testing-library/cypress

With TypeScript

Typings should be added as follows intsconfig.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 havecode-completion with the methods from this library, simply add the followingline to your project's root-leveljsconfig.json file:

{"include": ["node_modules/cypress","./cypress/**/*.js"]}

Usage

Cypress Testing Library extends Cypress'cy command.

Add this line to your project'scypress/support/commands.js:

import'@testing-library/cypress/add-commands'

You can now use all ofDOM Testing Library'sfindBy andfindAllBycommands.See theDOM Testing Library docs for reference

You can findall Library definitions here.

To configure DOM Testing Library, use the following custom command:

cy.configureCypressTestingLibrary(config)

To show some simple examples (fromcypress/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 elementcy.get('form').findAllByText('Button Text').should('exist')

Differences from DOM Testing Library

Cypress Testing Library supports both jQuery elements and DOM nodes. This isnecessary because Cypress uses jQuery elements, whileDOM Testing Libraryexpects DOM nodes. When you chain a query, it will get the first DOM node fromsubject of the collection and use that as thecontainer parameter for theDOM Testing Library functions.

query* queries are not supported. You should use theshould('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 ofDOM Testing Library, but instead forward to theget* queries and useCypress' built-in retryability using error messages fromget* APIs to forwardas error messages if a query fails.

findAll* can select more than one element and is closer in functionality tohow Cypress built-in commands work.find* commands will fail if more than oneelement is found that matches the criteria which is not how built-in Cypresscommands work, but is provided for closer compatibility to other TestingLibraries.

Cypress handles actions when there is only one element found. For example, thefollowing will work without having to limit to only 1 returned element. Thecy.click will automatically fail if more than 1 element is returned by thefindAllByText:

cy.findAllByText('Some Text').click()

If you intend to enforce only 1 element is returned by a selector, the followingexamples 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 fromdata-testId todata-test-id, add to your project'scypress/support/index.js:

import{configure}from'@testing-library/cypress'configure({testIdAttribute:'data-test-id'})

It accepts all configurations listed inDOM testing library.

Other Solutions

I'm not aware of any, if you are pleasemake a pull request and add ithere!

Issues

Looking to contribute? Look for theGood First Issuelabel.

🐛 Bugs

Please file an issue for bugs, missing documentation, or unexpected behavior.

See Bugs

💡 Feature Requests

Please file an issue to suggest new features. Vote on feature requests by addinga 👍. This helps maintainers prioritize what to work on.

See Feature Requests

Contributors ✨

Thanks goes to these people (emoji key):


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 theall-contributors specification.Contributions of any kind welcome!

LICENSE

MIT

About

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

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Contributors64


[8]ページ先頭

©2009-2025 Movatter.jp