• Stars
    star
    1,798
  • Rank 25,840 (Top 0.6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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

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 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 and add it here!

Issues

Looking to contribute? Look for the Good First Issue label.

๐Ÿ› 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 adding a ๐Ÿ‘. 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 the all-contributors specification. Contributions of any kind welcome!

LICENSE

MIT

More Repositories

1

react-testing-library

๐Ÿ Simple and complete React DOM testing utilities that encourage good testing practices.
JavaScript
18,952
star
2

react-hooks-testing-library

๐Ÿ Simple and complete React hooks testing utilities that encourage good testing practices.
TypeScript
5,083
star
3

jest-dom

๐Ÿฆ‰ Custom jest matchers to test the state of the DOM
JavaScript
4,422
star
4

dom-testing-library

๐Ÿ™ Simple and complete DOM testing utilities that encourage good testing practices.
JavaScript
3,271
star
5

user-event

๐Ÿ• Simulate user events
TypeScript
2,181
star
6

vue-testing-library

๐ŸฆŽ Simple and complete Vue.js testing utilities that encourage good testing practices.
JavaScript
1,071
star
7

eslint-plugin-testing-library

ESLint plugin to follow best practices and anticipate common mistakes when writing tests with Testing Library
TypeScript
979
star
8

testing-playground

Simple and complete DOM testing playground that encourage good testing practices.
JavaScript
776
star
9

angular-testing-library

๐Ÿฆ” Simple and complete Angular testing utilities that encourage good testing practices
TypeScript
626
star
10

svelte-testing-library

๐Ÿฟ๏ธ Simple and complete Svelte DOM testing utilities that encourage good testing practices
JavaScript
615
star
11

native-testing-library

๐Ÿณ Simple and complete React Native testing utilities that encourage good testing practices.
JavaScript
515
star
12

testing-library-docs

docs site for @testing-library/*
JavaScript
449
star
13

jest-native

๐Ÿฆ… Custom jest matchers to test the state of React Native
TypeScript
436
star
14

eslint-plugin-jest-dom

eslint rules for use with jest-dom
JavaScript
360
star
15

pptr-testing-library

puppeteer + dom-testing-library = ๐Ÿ’–
TypeScript
283
star
16

playwright-testing-library

๐Ÿ” Find elements in Playwright with queries from Testing Library
TypeScript
248
star
17

testing-library-recorder-extension

Testing Library Extension for Chrome DevTools Recorder
TypeScript
144
star
18

preact-testing-library

Simple and complete Preact DOM testing utilities that encourage good testing practices.
JavaScript
139
star
19

which-query

๐Ÿฆฉ Which query should I use?
CSS
124
star
20

testcafe-testing-library

๐Ÿ‚ Simple and complete custom Selectors for Testcafe that encourage good testing practices.
TypeScript
71
star
21

preact-hooks-testing-library

Simple and complete Preact hooks testing utilities that encourage good testing practices.
TypeScript
56
star
22

jasmine-dom

๐Ÿฆฅ Custom Jasmine matchers to test the state of the DOM
JavaScript
45
star
23

nightwatch-testing-library

๐Ÿฆ‡Simple and complete custom queries for Nightwatch that encourage good testing practices.
JavaScript
31
star
24

dom-testing-library-template

Template repository for bug reports to @testing-library/dom, @testing-library/react, and @testing-library/jest-dom
JavaScript
17
star
25

webdriverio-testing-library

๐Ÿ•ท๏ธ Simple and complete WebdriverIO DOM testing utilities that encourage good testing practices.
TypeScript
16
star
26

native-testing-library-docs

๐Ÿณ Docs site for native-testing-library
JavaScript
16
star
27

react-testing-library-help

Fork this repo to reproduce your issue
HTML
12
star
28

web-testing-library

๐Ÿ™ Experimental Web testing utilities that encourage good testing practices.
JavaScript
3
star