question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request: Cypress mount is chainable but not with testing-library

See original GitHub issue
  • cypress-testing-library version: 7.0.6
  • node version: 12.13.0
  • npm (or yarn) version: 6.12.0

Relevant code or config

mount(
    <Button data-testid="test-id">
        Button
    </Button>
).findByTestId('test-id');

What you did: Cypress v7 introduced the mount method for component testing. Mount is chainable and you can run cypress commands against it, like the following, for example:

mount(<Button onClick={onClick}>Button onClick</Button>)
    .get('.Button-root')
    .click()
    .get('@onClick')
    .should('be.called');

What happened: I tried to apply this concept to a test using Cypress Testing Library with the aforementioned code and it failed in finding the testid. The following, however, works:

mount(
    <Button data-testid="test-id">
        Button
    </Button>
);

cy.findByTestId('test-id');

Reproduction repository:

Problem description: You cannot chain cypress testing library calls off of mount.

Suggested solution: Add support for chaining off of mount to align with the rest of the Cypress API.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
NicholasBollcommented, May 11, 2021

I agree. If Cypress Testing Library was a React-specific Cypress plugin, I think it would make sense to add a cy.mount. But Cypress and Cypress Testing Library do not require React. This is probably why Cypress didn’t add a cy.mount command and instead require you to import mount and use it.

0reactions
reintroducingcommented, May 11, 2021

@NicholasBoll Your approach does indeed work, but I’m left questioning if this is even necessary. At this point I’m fine with the additional cy. and not adding additional complexity into the setup. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chained cypress-testing-library commands don't work as ...
I want to chain commands from cypress-testing-library, so that they are executed in the same context. In the example above I want to ......
Read more >
Cypress Testing Library
Cypress Testing Library supports both jQuery elements and DOM nodes. This is necessary because Cypress uses jQuery elements, while DOM Testing ...
Read more >
Test The Interface Not The Implementation - Gleb Bahmutov
The cypress.json file has all Cypress global configuration settings, where I enable component testing and fetch polyfill experimental features.
Read more >
Modern React testing, part 4: Cypress and ... - Artem Sapegin
You'll learn how to test React apps end-to-end with Cypress and Cypres Testing Library, how to mock network requests with Mock Service ...
Read more >
BUILDING RESILIENT SUPPLY CHAINS, REVITALIZING ...
will reduce supply chain vulnerabilities, and to work with America's ... qualified producers and they require extensive testing to meet ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found