feat: add no-add rule to disallow .and() in favor of .should()#310
feat: add no-add rule to disallow .and() in favor of .should()#310mschile merged 9 commits intocypress-io:masterfrom
Conversation
|
|
Hi @todd-m-kemp 👋🏼, thanks for opening this PR! I'll discuss with the team if we want a rule to completely disallow As a side note, even if we don't proceed with this rule, you can always create a custom rule for your needs. |
|
Hi @mschile! 👋 I don't think that this rule needs to be enabled by default. I did originally create a custom rule for my organization's use but felt that it could be a good rule for others who might want to use it as well which is what led me to create this PR.
Can you tell me more about this? Is this a special case somehow? I am not sure I understand why this scenario is of particular interest. |
|
@todd-m-kemp, I definitely see value in this rule as I agree that the following code doesn't read well: cy.get('elem')
.and('have.text', 'blah')However, I think this rule should not flag cy.get('.err')
.should('be.empty')
.and('be.hidden')cy.contains('Login')
.and('be.visible')Both of these examples are what's provided in the .and() documentation. Would you be willing/have the time to make these updates? |
@mschile Yes! I just pushed some changes up now. 👍 |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit eca7c7e. Configure here.
|
Thanks for the contribution @todd-m-kemp. I decided to rename the rule back to |
|
🎉 This PR is included in version 6.3.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |

Description
Add
no-addrule to disallow.and()in favour of.should().Cypress's
.and()is an alias for.should(). This rule enforces consistent use of.should()across Cypress test chains, and includes an auto-fixer that replaces.and()with.should()automatically.Changes:
lib/rules/no-add.js— new rule implementation with rootIsCy traversal to correctly identify Cypress chainstests/lib/rules/no-add.js— 20 tests covering valid and invalid cases including nested .within(), .then(), long chains, multiple .and() calls, and non-cy rootsdocs/rules/no-add.md— rule documentationlib/index.js— rule registered in plugin indexMotivation
We often see folks write code that looks something like this:
and after a PR review tells them that they don't need to explicitly check for visibility since they are clicking the element, they delete the visibility assertion and we end up with code that looks like this:
which is functional, but doesn't read well. To avoid code that looks like this, we would just like to disallow the use of
.and()suite-wide so that the use of.should()is required.Note
Medium Risk
Adds a new auto-fixable lint rule and refactors multiple existing rules to share a new
isRootCypresshelper, which could subtly change what gets flagged in Cypress call chains.Overview
Adds a new ESLint rule
cypress/no-andthat disallows starting assertion chains with.and()(unless it follows.should(),.and(), or.contains()), and auto-fixes offending cases by rewriting.andto.should.Introduces a shared
lib/utils/is-root-cypress.jshelper and updates several existing rules (assertion-before-screenshot,no-chained-get,no-debug,no-pause,unsafe-to-chain-command) to use it instead of local chain-walking implementations. Includes new documentation and a dedicated test suite forno-and, and registers the rule inlib/index.js.Reviewed by Cursor Bugbot for commit ee566f7. Bugbot is set up for automated code reviews on this repo. Configure here.