Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: Support chained wait in no-unnecessary-waiting rule
Catch chained wait
  • Loading branch information
chrisbreiding committed Feb 5, 2020
2 parents 475a26c + 9263d51 commit 3eb3d2e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/rules/no-unnecessary-waiting.js
Expand Up @@ -33,10 +33,19 @@ module.exports = {
},
}

function nodeIsCalledByCy (node) {
if (node.type === 'Identifier' && node.name === 'cy') return true

if (typeof node.callee === 'undefined' || typeof node.callee.object === 'undefined') {
return false
}

return nodeIsCalledByCy(node.callee.object)
}

function isCallingCyWait (node) {
return node.callee.type === 'MemberExpression' &&
node.callee.object.type === 'Identifier' &&
node.callee.object.name === 'cy' &&
nodeIsCalledByCy(node) &&
node.callee.property.type === 'Identifier' &&
node.callee.property.name === 'wait'
}
Expand All @@ -48,6 +57,8 @@ function isNumberArgument (node) {
}

function isIdentifierNumberConstArgument (node, scope) {
if (node.arguments.length === 0) return false

if (node.arguments[0].type !== 'Identifier') return false

const resolvedIdentifier = scope.resolve(node.arguments[0]).resolved
Expand Down
6 changes: 6 additions & 0 deletions tests/lib/rules/no-unnecessary-waiting.js
Expand Up @@ -10,6 +10,8 @@ const parserOptions = { ecmaVersion: 6 }

ruleTester.run('no-unnecessary-waiting', rule, {
valid: [
{ code: 'foo.wait(10)', parserOptions },

{ code: 'cy.wait("@someRequest")', parserOptions },
{ code: 'cy.wait("@someRequest", { log: false })', parserOptions },
{ code: 'cy.wait("@someRequest").then((xhr) => xhr)', parserOptions },
Expand All @@ -33,5 +35,9 @@ ruleTester.run('no-unnecessary-waiting', rule, {
{ code: 'const someNumber=500; cy.wait(someNumber)', parserOptions, errors },
{ code: 'function customWait (ms = 1) { cy.wait(ms) }', parserOptions, errors },
{ code: 'const customWait = (ms = 1) => { cy.wait(ms) }', parserOptions, errors },

{ code: 'cy.get(".some-element").wait(10)', parserOptions, errors },
{ code: 'cy.get(".some-element").contains("foo").wait(10)', parserOptions, errors },
{ code: 'const customWait = (ms = 1) => { cy.get(".some-element").wait(ms) }', parserOptions, errors },
],
})

0 comments on commit 3eb3d2e

Please sign in to comment.