Skip to content

Commit

Permalink
feat: Add handling for when the wait method is given an Identifier wh…
Browse files Browse the repository at this point in the history
…ich has a number value
  • Loading branch information
chrisbreiding authored Dec 13, 2019
2 parents 9086352 + 0ef8935 commit e66bdfd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/rules/no-unnecessary-waiting.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ module.exports = {
create (context) {
return {
CallExpression (node) {
if (isCallingCyWait(node) && isNumberArgument(node)) {
context.report({ node, messageId: 'unexpected' })
if (isCallingCyWait(node)) {
const scope = context.getScope()

if (isIdentifierNumberConstArgument(node, scope) || isNumberArgument(node)) {
context.report({ node, messageId: 'unexpected' })
}
}
},
}
Expand All @@ -42,3 +46,14 @@ function isNumberArgument (node) {
node.arguments[0].type === 'Literal' &&
typeof (node.arguments[0].value) === 'number'
}

function isIdentifierNumberConstArgument (node, scope) {
if (node.arguments[0].type !== 'Identifier') {
return false
}

const resolvedIdentifier = scope.resolve(node.arguments[0]).resolved
const IdentifierValue = resolvedIdentifier.defs[0].node.init.value

return typeof IdentifierValue === 'number'
}
3 changes: 3 additions & 0 deletions tests/lib/rules/no-unnecessary-waiting.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ ruleTester.run('no-unnecessary-waiting', rule, {
{ code: 'cy.clock(5000)', parserOptions },
{ code: 'cy.scrollTo(0, 10)', parserOptions },
{ code: 'cy.tick(500)', parserOptions },

{ code: 'const someRequest="@someRequest"; cy.wait(someRequest)', parserOptions, errors },
],

invalid: [
{ code: 'cy.wait(0)', parserOptions, errors },
{ code: 'cy.wait(100)', parserOptions, errors },
{ code: 'cy.wait(5000)', parserOptions, errors },
{ code: 'const someNumber=500; cy.wait(someNumber)', parserOptions, errors },
],
})

0 comments on commit e66bdfd

Please sign in to comment.