Skip to content

Commit ba8c879

Browse files
authored
feat: Add no-pause rule (#85)
1 parent 61a9de1 commit ba8c879

File tree

4 files changed

+108
-1
lines changed

4 files changed

+108
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ You can add rules:
3535
"cypress/no-unnecessary-waiting": "error",
3636
"cypress/assertion-before-screenshot": "warn",
3737
"cypress/no-force": "warn",
38-
"cypress/no-async-tests": "error"
38+
"cypress/no-async-tests": "error",
39+
"cypress/no-pause": "error"
3940
}
4041
}
4142
```
@@ -123,6 +124,7 @@ Rules with a check mark (✅) are enabled by default while using the `plugin:cyp
123124
| | [no-force](./docs/rules/no-force.md) | Disallow using `force: true` with action commands |
124125
| | [assertion-before-screenshot](./docs/rules/assertion-before-screenshot.md) | Ensure screenshots are preceded by an assertion |
125126
| | [require-data-selectors](./docs/rules/require-data-selectors.md) | Only allow data-\* attribute selectors (require-data-selectors) |
127+
| | [no-pause](./docs/rules/no-pause.md) | Disallow `cy.pause()` parent command |
126128

127129
## Chai and `no-unused-expressions`
128130

docs/rules/no-pause.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## Do not use `cy.pause` command
2+
3+
It is recommended to remove [cy.pause](https://on.cypress.io/pause) command before committing the specs to avoid other developers getting unexpected results.
4+
5+
Invalid:
6+
7+
```js
8+
cy.pause();
9+
```
10+
11+
Valid:
12+
13+
```js
14+
// only the parent cy.pause command is detected
15+
cy.get('selector').pause();
16+
```

lib/rules/no-pause.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict'
2+
3+
//------------------------------------------------------------------------------
4+
// Rule Definition
5+
//------------------------------------------------------------------------------
6+
7+
module.exports = {
8+
meta: {
9+
docs: {
10+
description: 'Disallow using of \'cy.pause\' calls',
11+
category: 'Possible Errors',
12+
recommended: false,
13+
},
14+
fixable: null, // or "code" or "whitespace"
15+
schema: [],
16+
messages: {
17+
unexpected: 'Do not use cy.pause command',
18+
},
19+
},
20+
21+
create (context) {
22+
23+
// variables should be defined here
24+
25+
//----------------------------------------------------------------------
26+
// Helpers
27+
//----------------------------------------------------------------------
28+
function isCallingPause (node) {
29+
return node.callee &&
30+
node.callee.property &&
31+
node.callee.property.type === 'Identifier' &&
32+
node.callee.property.name === 'pause'
33+
}
34+
35+
function isCypressCall (node) {
36+
return node.callee &&
37+
node.callee.type === 'MemberExpression' &&
38+
node.callee.object.type === 'Identifier' &&
39+
node.callee.object.name === 'cy'
40+
}
41+
42+
//----------------------------------------------------------------------
43+
// Public
44+
//----------------------------------------------------------------------
45+
46+
return {
47+
48+
CallExpression (node) {
49+
if (isCypressCall(node) && isCallingPause(node)) {
50+
context.report({ node, messageId: 'unexpected' })
51+
}
52+
},
53+
54+
}
55+
},
56+
}

tests/lib/rules/no-pause.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict'
2+
3+
//------------------------------------------------------------------------------
4+
// Requirements
5+
//------------------------------------------------------------------------------
6+
7+
const rule = require('../../../lib/rules/no-pause')
8+
9+
const RuleTester = require('eslint').RuleTester
10+
11+
const errors = [{ messageId: 'unexpected' }]
12+
const parserOptions = { ecmaVersion: 2018 }
13+
14+
//------------------------------------------------------------------------------
15+
// Tests
16+
//------------------------------------------------------------------------------
17+
18+
const ruleTester = new RuleTester()
19+
20+
ruleTester.run('no-pause', rule, {
21+
22+
valid: [
23+
// for now, we do not detect .pause() child command
24+
{ code: `cy.get('button').pause()`, parserOptions },
25+
{ code: `pause()`, parserOptions },
26+
{ code: `cy.get('button').dblclick()`, parserOptions },
27+
],
28+
29+
invalid: [
30+
{ code: `cy.pause()`, parserOptions, errors },
31+
{ code: `cy.pause({ log: false })`, parserOptions, errors },
32+
],
33+
})

0 commit comments

Comments
 (0)