Skip to content

Commit

Permalink
feat: Add no-pause rule (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov authored Sep 14, 2021
1 parent 61a9de1 commit ba8c879
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ You can add rules:
"cypress/no-unnecessary-waiting": "error",
"cypress/assertion-before-screenshot": "warn",
"cypress/no-force": "warn",
"cypress/no-async-tests": "error"
"cypress/no-async-tests": "error",
"cypress/no-pause": "error"
}
}
```
Expand Down Expand Up @@ -123,6 +124,7 @@ Rules with a check mark (✅) are enabled by default while using the `plugin:cyp
| | [no-force](./docs/rules/no-force.md) | Disallow using `force: true` with action commands |
| | [assertion-before-screenshot](./docs/rules/assertion-before-screenshot.md) | Ensure screenshots are preceded by an assertion |
| | [require-data-selectors](./docs/rules/require-data-selectors.md) | Only allow data-\* attribute selectors (require-data-selectors) |
| | [no-pause](./docs/rules/no-pause.md) | Disallow `cy.pause()` parent command |

## Chai and `no-unused-expressions`

Expand Down
16 changes: 16 additions & 0 deletions docs/rules/no-pause.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## Do not use `cy.pause` command

It is recommended to remove [cy.pause](https://on.cypress.io/pause) command before committing the specs to avoid other developers getting unexpected results.

Invalid:

```js
cy.pause();
```

Valid:

```js
// only the parent cy.pause command is detected
cy.get('selector').pause();
```
56 changes: 56 additions & 0 deletions lib/rules/no-pause.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict'

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

module.exports = {
meta: {
docs: {
description: 'Disallow using of \'cy.pause\' calls',
category: 'Possible Errors',
recommended: false,
},
fixable: null, // or "code" or "whitespace"
schema: [],
messages: {
unexpected: 'Do not use cy.pause command',
},
},

create (context) {

// variables should be defined here

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

function isCypressCall (node) {
return node.callee &&
node.callee.type === 'MemberExpression' &&
node.callee.object.type === 'Identifier' &&
node.callee.object.name === 'cy'
}

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

return {

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

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

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const rule = require('../../../lib/rules/no-pause')

const RuleTester = require('eslint').RuleTester

const errors = [{ messageId: 'unexpected' }]
const parserOptions = { ecmaVersion: 2018 }

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

const ruleTester = new RuleTester()

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

valid: [
// for now, we do not detect .pause() child command
{ code: `cy.get('button').pause()`, parserOptions },
{ code: `pause()`, parserOptions },
{ code: `cy.get('button').dblclick()`, parserOptions },
],

invalid: [
{ code: `cy.pause()`, parserOptions, errors },
{ code: `cy.pause({ log: false })`, parserOptions, errors },
],
})

0 comments on commit ba8c879

Please sign in to comment.