getBy* with .should('not.exist')
See original GitHub issueThe behavior of cypress-testing-library’s getBy
queries is a bit different than the cypress built-in .get() method: the built-in will retry should('not.exist')
until it times out, but the getBy
queries timeout on the query. One workaround is to change to getBy
to queryBy
, but this requires a little bit of extra dom-testing-library knowledge that can confuse people coming from Cypress, and still doesn’t retry.
Alternative
// This doesn't work
cy.getByTestId('my-item').should('not.exist');
// This works for verifying an element isn't present *right now*
cy.queryByTestId('my-item').should('be.null');
// This should retry but does not
cy.queryByTestId('my-item').should('not.exist');
Idea
It might make sense for queryBy*
to actually be aliased to getBy*
in the cypress context?
EDIT: actually the .should() chain should really trigger a retry of the parent command. Maybe custom commands don’t hook into .should correctly?
Related to #23, https://github.com/cypress-io/cypress/issues/3109
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
How do you test for the non-existence of an element using jest ...
From DOM Testing-library Docs - Appearance and Disappearance. Asserting elements are not present. The standard getBy methods throw an error ...
Read more >About Queries | Testing Library
Single Elements. getBy... : Returns the matching node for a query, and throw a descriptive error if no elements match or if more...
Read more >Testing Library - Queries - DEV Community
This article explains the three types of queries ("getBy", "findBy", "queryBy") and how to ... Asserting an element should not be present.
Read more >Guide to testing in React - my software development blog
getBy vs findBy vs queryBy; The act() function; Waiting for async actions ... You can check if an element does not exist by...
Read more >contains - Cypress Documentation
getCookies().contains('_key') // Errors, 'getCookies' does not yield DOM ... .contains() yields the new DOM element it found. .contains() is a query, ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’m thinking Cypress should support hooking into the retry mechanism for custom commands. Filed an issue to discuss: https://github.com/cypress-io/cypress/issues/3109
I’d be fine if anyone wants to try to make that happen. Then we can switch over to a built-in API when it becomes available.