October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

How to Use Testing Library with Cypress

Add Testing Library’s user-facing queries to Cypress with a support-file import, then use retryable findBy commands for accessible, scoped end-to-end tests.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Install @testing-library/cypress, import its command setup from Cypress’s support file, then use retryable cy.findBy… queries such as cy.findByRole() in your tests. Cypress must already be installed in the project. The integration adds Testing Library queries to Cypress; it does not replace Cypress’s normal command chain.

Install and register Cypress Testing Library

Install the package

From the project directory, add the integration as a development dependency:

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

Use the equivalent command for your package manager if the project uses something other than npm. Cypress itself must also be installed. Because Cypress’s supported Node.js versions, operating systems, browsers, and package managers can change, check its current installation guide for requirements that match your environment.

Import the commands in Cypress support

Add this import to the Cypress support commands file, typically cypress/support/commands.js:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import '@testing-library/cypress/add-commands'

Make sure the file is part of the support-file configuration for the test type you are running. Once loaded, the integration adds its supported queries to cy.

TypeScript projects

If TypeScript does not recognize the added Cypress commands, follow the integration’s TypeScript setup: add both cypress and @testing-library/cypress to the types array in compilerOptions in tsconfig.json. The exact shape of that file depends on the project’s existing configuration.

Write tests with retryable semantic queries

Use findBy or findAllBy query commands from cy, then continue with Cypress actions and assertions. For example:

cy.findByRole('button', { name: /save/i }).click()

cy.findByRole('dialog').within(() => {
  cy.findByRole('button', { name: /confirm/i }).should('exist')
})

Role and accessible name queries can express how a person would identify a control. They also make the test’s intent legible: the first query looks for a button called “Save,” rather than relying on a styling class or a particular DOM structure. Use a query only when its role, name, or other accessible information matches what the application actually renders.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Wait for content that appears asynchronously

The findBy family works with Cypress retryability, so Cypress can retry the query while the page updates instead of requiring a fixed sleep for ordinary asynchronous rendering. For example, after an action that opens a dialog, query for the dialog or its control with findByRole. Cypress’s retry behavior still depends on its command and timeout settings; a query cannot make content appear if the application never renders it.

The integration guide supports querying within Cypress chains and documents handling both jQuery elements and DOM nodes. You can scope a query to a form, for example:

cy.get('form').findByRole('button', { name: /submit/i }).click()

Scoping can be useful when a page has several controls with the same accessible name. For a dialog or other Cypress subject, .within() is another clear way to restrict the commands that follow.

Choose selectors to match the test’s purpose

Cypress’s migration guidance describes semantic locators and application-provided data attributes as options, not as a universal contest with one winner:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Selector approach Useful when Trade-off
Testing Library semantic query, such as findByRole, findByLabelText, or findByText The test should identify a control or content as a user would, and its role, label, or text is meaningful. Queries depend on the rendered accessible role, label, or text; a change to that user-facing content can require a test update.
Application data attribute, such as data-testid or data-cy The project already uses dedicated test attributes, or the interaction has no suitable user-facing identifier. The attribute may need to be added to application markup; the selector does not itself describe the user-visible meaning of the element.

Cypress’s migration guidance maps role, label, text, and placeholder locators to the corresponding Testing Library commands, and also describes data attributes as an alternative. Keep to the project’s conventions where they serve the test; prefer a semantic query when it accurately captures the interaction being tested.

Queries to use—and version-sensitive caveats

The Cypress Testing Library guide documents findBy and findAllBy query commands and says that get* queries are not supported. It also says query* queries are no longer needed since version 5 and are slated for removal in version 6. That note is version-sensitive: check the guide and the installed @testing-library/cypress version before relying on it, especially when upgrading.

Testing Library’s general query guide explains that query families differ in whether they throw when there is no match, return no match, or retry for asynchronous content. For Cypress integration, follow its supported command pattern rather than assuming every query family from other Testing Library environments is available.

Configure the integration when necessary

If the defaults do not suit the project, the integration provides cy.configureCypressTestingLibrary(config). Check the official repository documentation for the configuration options supported by the installed release; do not assume an option from another Testing Library integration applies here.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot common setup and query failures

  • findByRole is not a function: Verify that @testing-library/cypress is installed and that import '@testing-library/cypress/add-commands' runs from the support file configured for the tests you are executing. Restart the Cypress process after dependency or support-file changes.
  • TypeScript reports that a query does not exist on cy: Add cypress and @testing-library/cypress to compilerOptions.types in tsconfig.json, as described in the integration guide, and confirm that the project is using the expected TypeScript configuration.
  • A query times out or finds no element: Check that the element is rendered in the current page state and that the role, accessible name, label, or text matches the UI. If the element appears after an action, query after that action; avoid substituting a fixed delay for a query that can retry.
  • A page has several matching controls: Scope the query to a form or use .within() on a dialog or other container, or make the accessible name specific enough to identify the intended control.
  • An older example uses getBy… or queryBy…: Check the installed integration version and its current guide. Its documented Cypress pattern is findBy/findAllBy; the guide says get* is unsupported and notes the version-sensitive status of query*.
  • Cypress itself fails to install or launch: Check the current Cypress installation requirements for your Node.js version, operating system, browser, and package manager. Historical Cypress major-version changes can affect setup, so diagnose against the version in the project rather than an old tutorial.

Or skip the browser setup

If you need screenshots rather than an interactive Cypress test, ScreenshotNeo returns a screenshot or PDF from one GET request. Its clean-shot options can accept consent banners and remove more than 60 known consent platforms, newsletter popups, and chat widgets before capture. Bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers identify the page verdict and billing status. An MCP server exposes screenshot and page-info tools to AI agents. The free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots.

See the ScreenshotNeo API documentation. Example request:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

Sign up free for 1,000 screenshots a month, with no card required.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.