feat: add no-add rule to disallow .and() in favor of .should()#310
Open
todd-m-kemp wants to merge 2 commits intocypress-io:masterfrom
Open
feat: add no-add rule to disallow .and() in favor of .should()#310todd-m-kemp wants to merge 2 commits intocypress-io:masterfrom
todd-m-kemp wants to merge 2 commits intocypress-io:masterfrom
Conversation
|
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.
todd-m-kemp
commented
Mar 30, 2026
Comment on lines
+34
to
+51
| function rootIsCy(node) { | ||
| let current = node.callee.object | ||
| while (current) { | ||
| if (current.type === 'Identifier' && current.name === 'cy') { | ||
| return true | ||
| } | ||
| if (current.type === 'CallExpression') { | ||
| current = current.callee.object | ||
| } | ||
| else if (current.type === 'MemberExpression') { | ||
| current = current.object | ||
| } | ||
| else { | ||
| break | ||
| } | ||
| } | ||
| return false | ||
| } |
Author
There was a problem hiding this comment.
I was looking at other PRs and came across this comment.
The approach I took here works and is suitable for this use case but for code cleanliness reasons I expect that I should just use that approach instead. It's likely worth waiting for that to work to be completed and merged in and updating accordingly here.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

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
Low Risk
Adds an opt-in lint rule with a straightforward string replacement auto-fix, with scope limited to Cypress-rooted call chains and covered by unit tests.
Overview
Adds a new
cypress/no-andrule that reports.and()calls specifically within Cypress command chains and provides an auto-fix that rewrites.and(...)to.should(...).Registers the rule in the plugin index, adds rule docs, and introduces a dedicated test suite covering both Cypress and non-Cypress call chains (including nested/long chains and multiple
.and()occurrences).Written by Cursor Bugbot for commit b722323. This will update automatically on new commits. Configure here.