MCPcopy
hub / github.com/testing-library/cypress-testing-library

github.com/testing-library/cypress-testing-library @v10.1.3 sqlite

repository ↗ · DeepWiki ↗ · release v10.1.3 ↗
26 symbols 67 edges 16 files 17 documented · 65%
README

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][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] [![PRs Welcome][prs-badge]][prs] [![Code of Conduct][coc-badge]][coc]

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

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

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:

{
  "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 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

Extension points exported contracts — how you extend this code

CTLMatcherOptions (Interface)
(no doc)
types/index.d.ts
Chainable (Interface)
(no doc)
types/index.d.ts

Core symbols most depended-on inside this repo

findByText
called by 23
types/index.d.ts
findAllByText
called by 3
types/index.d.ts
findByLabelText
called by 3
types/index.d.ts
findByPlaceholderText
called by 2
types/index.d.ts
findAllByPlaceholderText
called by 2
types/index.d.ts
findAllByLabelText
called by 2
types/index.d.ts
findByAltText
called by 2
types/index.d.ts
findAllByAltText
called by 2
types/index.d.ts

Shape

Method 17
Function 7
Interface 2

Languages

TypeScript100%

Modules by API surface

types/index.d.ts19 symbols
src/index.js5 symbols
src/utils.js1 symbols
cypress/e2e/find.cy.js1 symbols

Dependencies from manifests, versioned

@babel/runtime7.14.6 · 1×
@testing-library/dom10.1.0 · 1×
cypress15.0.0 · 1×
kcd-scripts11.2.0 · 1×
npm-run-all4.1.5 · 1×
typescript4.3.5 · 1×

For agents

$ claude mcp add cypress-testing-library \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact