From ba8c8797068b4e59f74c43af548f068624006482 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Tue, 14 Sep 2021 10:51:16 -0400 Subject: [PATCH] feat: Add no-pause rule (#85) --- README.md | 4 ++- docs/rules/no-pause.md | 16 +++++++++++ lib/rules/no-pause.js | 56 +++++++++++++++++++++++++++++++++++++ tests/lib/rules/no-pause.js | 33 ++++++++++++++++++++++ 4 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 docs/rules/no-pause.md create mode 100644 lib/rules/no-pause.js create mode 100644 tests/lib/rules/no-pause.js diff --git a/README.md b/README.md index 8f68a255..4ad2b5e6 100644 --- a/README.md +++ b/README.md @@ -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" } } ``` @@ -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` diff --git a/docs/rules/no-pause.md b/docs/rules/no-pause.md new file mode 100644 index 00000000..757c332c --- /dev/null +++ b/docs/rules/no-pause.md @@ -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(); +``` diff --git a/lib/rules/no-pause.js b/lib/rules/no-pause.js new file mode 100644 index 00000000..0306edea --- /dev/null +++ b/lib/rules/no-pause.js @@ -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' }) + } + }, + + } + }, +} diff --git a/tests/lib/rules/no-pause.js b/tests/lib/rules/no-pause.js new file mode 100644 index 00000000..24c2b503 --- /dev/null +++ b/tests/lib/rules/no-pause.js @@ -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 }, + ], +})