Skip to content

fix: conditional expressions are now checked by require-data-selectors - #305

Merged
cacieprins merged 3 commits into
cypress-io:masterfrom
MgmClientGuy0:require-data-selectors-conditional
Mar 24, 2026
Merged

cacieprins merged 3 commits into
cypress-io:masterfrom
MgmClientGuy0:require-data-selectors-conditional

Conversation

@MgmClientGuy0

@MgmClientGuy0 MgmClientGuy0 commented Mar 13, 2026

Copy link
Copy Markdown
Contributor

We had some places in our codebase where we passed a conditional as the selector in 'cy.get'. Both branches conformed to the rule, but it was still flagged. Obviously, tracking a value across complex if/else branches is potentially impossible, but some false positives can be reduced by checking conditional expressions. This PR adds support for that, by checking each branch recursively against the current rules. This supports nested conditionals, conditionals assigned to variables, variables used in conditionals, etc. Documentation and tests are extended.

The following code was flagged before and is not anymore:

cy.get(condition ? "[data-role='switch']" : "[data-id='123']")

Note

Low Risk
Low risk: small, localized change to an ESLint rule’s AST checking plus test/doc updates; main risk is unintended allow/deny behavior for some selector expressions.

Overview
Fixes cypress/require-data-selectors false positives when cy.get() receives a ternary/conditional selector by recursively validating both branches (including nested conditionals and variables).

Updates rule docs and expands test coverage with new valid/invalid cases for conditional expressions, including alias branches and conditionals assigned to variables.

Written by Cursor Bugbot for commit c52556d. This will update automatically on new commits. Configure here.

@cypress-app-bot

Copy link
Copy Markdown
Collaborator

@MikeMcC399

Copy link
Copy Markdown
Collaborator

This proposed change looks like it goes in the direction of Conditional Testing. Since I don't have experience in this area, I would defer to the Cypress.io team to judge whether the added functionality is endorsed and encouraged, or perhaps to be discouraged.

@MgmClientGuy0

Copy link
Copy Markdown
Contributor Author

The code does not necessarily have to be used in conditional testing. We have custom commands that use a parameter in a conditional to support different use cases. The test itself passes a static parameter.

@cacieprins
cacieprins self-requested a review March 17, 2026 14:20
@cacieprins cacieprins self-assigned this Mar 17, 2026
@cacieprins cacieprins changed the title feat: Support ConditionalExpression in require-data-selectors fix: conditional expressions are now checked by require-data-selectors Mar 20, 2026
@cacieprins

Copy link
Copy Markdown
Contributor

While this does appear to endorse conditional expressions, it's a small change and does address the undefined behavior here.

A no-conditional-testing rule would be quite nice...

@cacieprins
cacieprins merged commit d69d417 into cypress-io:master Mar 24, 2026
14 checks passed
@cypress-app-bot

Copy link
Copy Markdown
Collaborator

🎉 This PR is included in version 6.2.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants