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

feat: add rule no-debug #216

Merged
merged 3 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ You can add rules:
"cypress/no-force": "warn",
"cypress/no-async-tests": "error",
"cypress/no-async-before": "error",
"cypress/no-pause": "error"
"cypress/no-pause": "error",
"cypress/no-debug": "error"
}
}
```
Expand Down Expand Up @@ -129,6 +130,7 @@ These rules enforce some of the [best practices recommended for using Cypress](h
| [no-assigning-return-values](docs/rules/no-assigning-return-values.md) | disallow assigning return values of `cy` calls | ✅ |
| [no-async-before](docs/rules/no-async-before.md) | disallow using `async`/`await` in Cypress `before` methods | |
| [no-async-tests](docs/rules/no-async-tests.md) | disallow using `async`/`await` in Cypress test cases | ✅ |
| [no-debug](docs/rules/no-debug.md) | disallow using `cy.debug()` calls | |
| [no-force](docs/rules/no-force.md) | disallow using `force: true` with action commands | |
| [no-pause](docs/rules/no-pause.md) | disallow using `cy.pause()` calls | |
| [no-unnecessary-waiting](docs/rules/no-unnecessary-waiting.md) | disallow waiting for arbitrary time periods | ✅ |
Expand Down
19 changes: 19 additions & 0 deletions docs/rules/no-debug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Disallow using `cy.debug()` calls (`cypress/no-debug`)

<!-- end auto-generated rule header -->
It is recommended to remove any [cy.debug](https://on.cypress.io/debug) commands before committing specs to avoid other developers getting unexpected results.

## Rule Details

Examples of **incorrect** code for this rule:

```js
cy.debug();
cy.get('selector').debug();
```

Examples of **correct** code for this rule:

```js
cy.get('selector')
```
1 change: 1 addition & 0 deletions legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
'require-data-selectors': require('./lib/rules/require-data-selectors'),
'no-force': require('./lib/rules/no-force'),
'no-pause': require('./lib/rules/no-pause'),
'no-debug': require('./lib/rules/no-debug'),
},
configs: {
recommended: require('./lib/config/recommended'),
Expand Down
1 change: 1 addition & 0 deletions lib/flat.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const plugin = {
'require-data-selectors': require('./rules/require-data-selectors'),
'no-force': require('./rules/no-force'),
'no-pause': require('./rules/no-pause'),
'no-debug': require('./rules/no-debug'),
},
}

Expand Down
61 changes: 61 additions & 0 deletions lib/rules/no-debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict'

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow using `cy.debug()` calls',
category: 'Possible Errors',
recommended: false,
url: 'https://github.com/cypress-io/eslint-plugin-cypress/blob/master/docs/rules/no-debug.md',
},
fixable: null, // or "code" or "whitespace"
schema: [],
messages: {
unexpected: 'Do not use cy.debug command',
},
},

create (context) {

// variables should be defined here

//----------------------------------------------------------------------
// Helpers
//----------------------------------------------------------------------
function isCallingDebug (node) {
return node.callee &&
node.callee.property &&
node.callee.property.type === 'Identifier' &&
node.callee.property.name === 'debug'
}

function isCypressCall (node) {
if (!node.callee || node.callee.type !== 'MemberExpression') {
return false;
}
if (node.callee.object.type === 'Identifier' && node.callee.object.name === 'cy') {
return true;
}
return isCypressCall(node.callee.object);
}

//----------------------------------------------------------------------
// Public
//----------------------------------------------------------------------

return {

CallExpression (node) {
if (isCypressCall(node) && isCallingDebug(node)) {
context.report({ node, messageId: 'unexpected' })
}
},

}
},
}
23 changes: 23 additions & 0 deletions tests/lib/rules/no-debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

const rule = require('../../../lib/rules/no-debug')
const RuleTester = require('eslint').RuleTester

const ruleTester = new RuleTester()

const errors = [{ messageId: 'unexpected' }]

ruleTester.run('no-debug', rule, {

valid: [
{ code: `debug()` },
{ code: `cy.get('button').dblclick()` },
],

invalid: [
{ code: `cy.debug()`, errors },
{ code: `cy.debug({ log: false })`, errors },
{ code: `cy.get('button').debug()`, errors },
{ code: `cy.get('a').should('have.attr', 'href').and('match', /dashboard/).debug()`, errors }
],
})