Skip to content

Commit

Permalink
feat: add rule no-debug
Browse files Browse the repository at this point in the history
  • Loading branch information
nicooprat committed Jul 31, 2024
1 parent e644392 commit 033de6e
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
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 }
],
})

0 comments on commit 033de6e

Please sign in to comment.