Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to skip remaining commands when subject is missing #29742

Open
steinybot opened this issue Jun 25, 2024 · 0 comments
Open

Add option to skip remaining commands when subject is missing #29742

steinybot opened this issue Jun 25, 2024 · 0 comments
Labels
type: feature New feature that does not currently exist

Comments

@steinybot
Copy link

steinybot commented Jun 25, 2024

What would you like?

I would like to do conditional testing. Well not really, I want conditional commands, I don't care about the assertions.

One solution is to have an option that prevents commands from failing with there are no results. For example:

cy.contains('Done', { iHaveReadTheDocsAndYesIReallyDoWantConditionalTestingAndTakeFullResponsibilityForMyActions: true })
  .then((el) => {
    if (el.length > 0) {
      ...
    }
  })

Ok, the option name is a bit facetious but in all seriousness, make it as long and cumbersome as you like in order to encourage people to only use it when they absolutely have to.

Why is this needed?

I am using Percy to take snapshots of the DOM. I am already going to whatever lengths necessary to ensure that the DOM is stable. This is a given and there is no way around it.

The normal commands that retry are exactly the tools we need to reliably get into a stable state.

Once in this stable state I need to be able to use various commands to select DOM elements and modify their contents so that I get a consistent screenshot. For example, dates and times and other randomly generated test data.

I want a single snapshot function which can conditionally select these elements and if they exist then modify them. It of course should not fail when they do not exist. This one function needs to be used from anywhere within my application. Without this I would have to write a bespoke snapshot function for every single possible state that my application could be in.

The draconian restrictions on conditional testing are making it much more difficult to write good tests. We end up with a lot of boilerplate code that is costly to maintain, long jQuery queries that attempt to replicate build-in cypress commands which are never as good, and our visual regression testing is more flaky.

Other

https://github.com/bahmutov/cypress-if is a pretty decent alternative but it only supports certain commands as it has to override them.

I've also hacked together my own version of testing-libraries queryBy... commands which short circuit the remaining commands like cypress-if when there are no results.

Related to #3757 but this is an actual proposal.

Please consider this seriously. This is by far my biggest issue with Cypress and I run into it in some form every single time I use it. I get the point you are trying to make and I agree with 99% of it but the reality is that there are cases where restricting conditional testing is making things more flaky. We end up resorting to worse workarounds. Retriability doesn't eliminate all flakiness anyway so it's a futile vendeta.

@jennifer-shehane jennifer-shehane added the type: feature New feature that does not currently exist label Jun 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature New feature that does not currently exist
Projects
None yet
Development

No branches or pull requests

2 participants