-
Notifications
You must be signed in to change notification settings - Fork 97
feat: add no-add rule to disallow .and() in favor of .should() #310
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
Open
todd-m-kemp
wants to merge
2
commits into
cypress-io:master
Choose a base branch
from
todd-m-kemp:tk/new-rule-no-and
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+210
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # cypress/no-and | ||
|
|
||
| 📝 Disallow the use of `.and()`. | ||
|
|
||
| 🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). | ||
|
|
||
| <!-- end auto-generated rule header --> | ||
|
|
||
| Cypress's [.and()](https://on.cypress.io/and) is an alias for [.should()](https://on.cypress.io/should). Using `.should()` consistently makes assertions easier to read and avoids ambiguity. | ||
|
|
||
| ## Rule Details | ||
|
|
||
| This rule disallows the use of `.and()` in Cypress chains and auto-fixes it to `.should()`. | ||
|
|
||
| Examples of **incorrect** code for this rule: | ||
|
|
||
| ```js | ||
| cy.get('foo').and('be.visible') | ||
| cy.get('foo').should('be.visible').and('have.text', 'bar') | ||
| cy.contains('Submit').and('be.disabled') | ||
| cy.get('input').invoke('val').and('eq', 'hello') | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule: | ||
|
|
||
| ```js | ||
| cy.get('foo').should('be.visible') | ||
| cy.get('foo').should('be.visible').should('have.text', 'bar') | ||
| cy.contains('Submit').should('be.disabled') | ||
| cy.get('input').invoke('val').should('eq', 'hello') | ||
| ``` | ||
|
|
||
| ## When Not To Use It | ||
|
|
||
| If you prefer using `.and()` for readability in chained assertions, turn this rule off. |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /** | ||
| * @fileoverview disallow the use of .and() | ||
| * @author Todd Kemp | ||
| */ | ||
| 'use strict' | ||
|
|
||
| // ------------------------------------------------------------------------------ | ||
| // Rule Definition | ||
| // ------------------------------------------------------------------------------ | ||
|
|
||
| /** @type {import('eslint').Rule.RuleModule} */ | ||
| module.exports = { | ||
| meta: { | ||
| type: 'suggestion', | ||
| docs: { | ||
| description: 'disallow the use of .and()', | ||
| recommended: false, | ||
| url: null, // URL to the documentation page for this rule | ||
| }, | ||
| fixable: 'code', | ||
| schema: [], // Add a schema if the rule has options | ||
| messages: { | ||
| unexpected: 'Do not use .and(); use .should() instead', | ||
| }, | ||
| }, | ||
|
|
||
| create(context) { | ||
| // variables should be defined here | ||
|
|
||
| // ---------------------------------------------------------------------- | ||
| // Helpers | ||
| // ---------------------------------------------------------------------- | ||
|
|
||
| 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 | ||
| } | ||
|
|
||
| // ---------------------------------------------------------------------- | ||
| // Public | ||
| // ---------------------------------------------------------------------- | ||
|
|
||
| return { | ||
| CallExpression(node) { | ||
| if ( | ||
| node.callee.type === 'MemberExpression' | ||
| && node.callee.property.name === 'and' | ||
| && rootIsCy(node) | ||
| ) { | ||
| context.report({ | ||
| node, | ||
| messageId: 'unexpected', | ||
| fix(fixer) { | ||
| return fixer.replaceText(node.callee.property, 'should') | ||
| }, | ||
| }) | ||
| } | ||
| }, | ||
| } | ||
| }, | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| /** | ||
| * @fileoverview disallow the use of .and() | ||
| * @author Todd Kemp | ||
| */ | ||
| 'use strict' | ||
|
|
||
| // ------------------------------------------------------------------------------ | ||
| // Requirements | ||
| // ------------------------------------------------------------------------------ | ||
|
|
||
| const rule = require('../../../lib/rules/no-and'), | ||
| RuleTester = require('eslint').RuleTester | ||
|
|
||
| // ------------------------------------------------------------------------------ | ||
| // Tests | ||
| // ------------------------------------------------------------------------------ | ||
|
|
||
| const ruleTester = new RuleTester() | ||
| const errors = [{ messageId: 'unexpected' }] | ||
|
|
||
| ruleTester.run('no-and', rule, { | ||
| valid: [ | ||
| { code: 'cy.get(\'foo\').should(\'be.visible\')' }, | ||
| { code: 'cy.get(\'foo\').should(\'be.visible\').should(\'have.text\', \'bar\')' }, | ||
| { code: 'cy.get(\'foo\').find(\'.bar\').should(\'have.class\', \'active\')' }, | ||
| { code: 'someOtherLib.and(\'something\')' }, | ||
| { code: 'someOtherLib.get(\'foo\').and(\'be.visible\')' }, | ||
| { code: 'expect(foo).to.equal(true).and(\'have.text\', \'bar\')' }, | ||
| { code: 'someOtherLib.get(\'foo\').find(\'.bar\').filter(\'.baz\').first().and(\'be.visible\')' }, | ||
| ], | ||
|
|
||
| invalid: [ | ||
| { | ||
| code: 'cy.and(\'be.visible\')', | ||
| output: 'cy.should(\'be.visible\')', | ||
| errors, | ||
| }, | ||
| { | ||
| code: 'cy.get(\'foo\').and(\'be.visible\')', | ||
| output: 'cy.get(\'foo\').should(\'be.visible\')', | ||
| errors, | ||
| }, | ||
| { | ||
| code: 'cy.get(\'foo\').should(\'be.visible\').and(\'have.text\', \'bar\')', | ||
| output: 'cy.get(\'foo\').should(\'be.visible\').should(\'have.text\', \'bar\')', | ||
| errors, | ||
| }, | ||
| { | ||
| code: 'cy.get(\'foo\').find(\'.bar\').and(\'have.class\', \'active\')', | ||
| output: 'cy.get(\'foo\').find(\'.bar\').should(\'have.class\', \'active\')', | ||
| errors, | ||
| }, | ||
| { | ||
| code: 'cy.get(\'foo\').find(\'.bar\').filter(\'.baz\').first().and(\'be.visible\')', | ||
| output: 'cy.get(\'foo\').find(\'.bar\').filter(\'.baz\').first().should(\'be.visible\')', | ||
| errors, | ||
| }, | ||
| { | ||
| code: 'cy.get(\'foo\').find(\'.bar\').filter(\'.baz\').first().parent().siblings().eq(0).and(\'be.visible\')', | ||
| output: 'cy.get(\'foo\').find(\'.bar\').filter(\'.baz\').first().parent().siblings().eq(0).should(\'be.visible\')', | ||
| errors, | ||
| }, | ||
| { | ||
| code: 'cy.get(\'.container\').within(() => { cy.get(\'.item\').and(\'be.visible\') })', | ||
| output: 'cy.get(\'.container\').within(() => { cy.get(\'.item\').should(\'be.visible\') })', | ||
| errors, | ||
| }, | ||
| { | ||
| code: 'cy.get(\'foo\').then(($el) => { cy.wrap($el).and(\'have.class\', \'active\') })', | ||
| output: 'cy.get(\'foo\').then(($el) => { cy.wrap($el).should(\'have.class\', \'active\') })', | ||
| errors, | ||
| }, | ||
| { | ||
| code: 'cy.get(\'foo\').then(($el) => {}).and(\'be.visible\')', | ||
| output: 'cy.get(\'foo\').then(($el) => {}).should(\'be.visible\')', | ||
| errors, | ||
| }, | ||
| { | ||
| code: 'cy.get(\'foo\').as(\'myEl\').and(\'be.visible\')', | ||
| output: 'cy.get(\'foo\').as(\'myEl\').should(\'be.visible\')', | ||
| errors, | ||
| }, | ||
| { | ||
| code: 'cy.get(\'foo\').and(\'be.visible\').and(\'have.text\', \'bar\')', | ||
| output: 'cy.get(\'foo\').should(\'be.visible\').should(\'have.text\', \'bar\')', | ||
| errors: [{ messageId: 'unexpected' }, { messageId: 'unexpected' }], | ||
| }, | ||
| { | ||
| code: 'cy.contains(\'Submit\').and(\'be.disabled\')', | ||
| output: 'cy.contains(\'Submit\').should(\'be.disabled\')', | ||
| errors, | ||
| }, | ||
| { | ||
| code: 'cy.get(\'input\').invoke(\'val\').and(\'eq\', \'hello\')', | ||
| output: 'cy.get(\'input\').invoke(\'val\').should(\'eq\', \'hello\')', | ||
| errors, | ||
| }, | ||
| ], | ||
| }) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.