Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
@testing-library/cypress
Advanced tools
Simple and complete custom Cypress commands and utilities that encourage good testing practices.
@testing-library/cypress is an extension of the Testing Library family for use with Cypress, a popular end-to-end testing framework. It provides a set of custom commands for interacting with DOM elements in a way that resembles how users interact with your application, making tests more readable and maintainable.
Querying Elements
This feature allows you to query DOM elements using the same queries available in @testing-library/dom. For example, you can find a button with the text 'Submit' and click it.
cy.findByText('Submit').click();
Assertions
You can make assertions on elements found using Testing Library queries. This example checks if a button with the role 'button' and name 'submit' is visible.
cy.findByRole('button', { name: /submit/i }).should('be.visible');
Custom Commands
The package provides custom commands like `findByLabelText` to interact with form elements. This example types 'myUsername' into an input field labeled 'Username'.
cy.findByLabelText('Username').type('myUsername');
cypress-testing-library is another package that integrates Testing Library queries with Cypress. It offers similar functionalities to @testing-library/cypress, allowing you to use Testing Library's queries within Cypress tests.
cypress-axe is a plugin for Cypress that integrates the axe-core accessibility testing engine. While it focuses on accessibility testing rather than user interaction, it complements @testing-library/cypress by ensuring your application is accessible.
cypress-cucumber-preprocessor allows you to write Cypress tests using the Gherkin syntax. While it doesn't provide the same querying capabilities as @testing-library/cypress, it offers a different approach to writing readable and maintainable tests.
Simple and complete custom Cypress commands and utilities that encourage good testing practices.
You want to use DOM Testing Library
methods in your
Cypress tests.
This allows you to use all the useful
DOM Testing Library
methods in your tests.
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
Typings should be added as follows in tsconfig.json
:
{
"compilerOptions": {
"types": ["cypress", "@testing-library/cypress"]
}
}
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"]
}
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/integration/find.spec.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')
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')
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.
I'm not aware of any, if you are please make a pull request and add it here!
Thanks goes to these people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
MIT
FAQs
Simple and complete custom Cypress commands and utilities that encourage good testing practices.
The npm package @testing-library/cypress receives a total of 642,436 weekly downloads. As such, @testing-library/cypress popularity was classified as popular.
We found that @testing-library/cypress demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.ย It has 15 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.