GithubHelp home page GithubHelp logo

travi / cypress-testing-library Goto Github PK

View Code? Open in Web Editor NEW

This project forked from testing-library/cypress-testing-library

0.0 2.0 0.0 573 KB

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

Home Page: http://npm.im/@testing-library/cypress

License: MIT License

JavaScript 85.09% HTML 14.91%

cypress-testing-library's Introduction

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 in @types/testing-library__cypress at DefinitelyTyped, and should be added as follows in tsconfig.json:

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

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, findAllBy, queryBy and queryAllBy 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/integration/query.spec.js or cypress/integration/find.spec.js):

cy.queryAllByText('Button Text').should('exist')
cy.queryAllByText('Non-existing Button Text').should('not.exist')
cy.queryAllByLabelText('Label text', {timeout: 7000}).should('exist')
cy.findAllByText('Jackie Chan').click();

// findAllByText _inside_ a form element
cy.get('form').within(() => {
  cy.findAllByText('Button Text').should('exist')
})
cy.get('form').then(subject => {
  cy.findAllByText('Button Text', {container: subject}).should('exist')
})
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 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.

get* queries are disabled. 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. query* also uses get* APIs, but disables retryability.

findAll* can select more than one element and is closer in functionality to how Cypress built-in commands work. findAll* is preferred to find* queries. 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')

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

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

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

πŸ› πŸ’» ⚠️

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

LICENSE

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.