Skip to content

Commit

Permalink
fix: Handle cy.wait in a function with a default parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbreiding committed Dec 16, 2019
1 parent e66bdfd commit 475a26c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
25 changes: 20 additions & 5 deletions lib/rules/no-unnecessary-waiting.js
Expand Up @@ -48,12 +48,27 @@ function isNumberArgument (node) {
}

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

const resolvedIdentifier = scope.resolve(node.arguments[0]).resolved
const IdentifierValue = resolvedIdentifier.defs[0].node.init.value
const definition = resolvedIdentifier.defs[0]
const isVariable = definition.type === 'Variable'

// const amount = 1000 or const amount = '@alias'
// cy.wait(amount)
if (isVariable) {
if (!definition.node.init) return false

return typeof definition.node.init.value === 'number'
}

const param = definition.node.params[definition.index]

// function wait (amount) { cy.wait(amount) }
// we can't know the type of value, so don't fail
if (!param || param.type !== 'AssignmentPattern') return false

return typeof IdentifierValue === 'number'
// function wait (amount = 1) { cy.wait(amount) } or
// function wait (amount = '@alias') { cy.wait(amount) }
return typeof param.right.value === 'number'
}
6 changes: 6 additions & 0 deletions tests/lib/rules/no-unnecessary-waiting.js
Expand Up @@ -20,12 +20,18 @@ ruleTester.run('no-unnecessary-waiting', rule, {
{ code: 'cy.tick(500)', parserOptions },

{ code: 'const someRequest="@someRequest"; cy.wait(someRequest)', parserOptions, errors },
{ code: 'function customWait (alias = "@someRequest") { cy.wait(alias) }', parserOptions, errors },
{ code: 'const customWait = (alias = "@someRequest") => { cy.wait(alias) }', parserOptions, errors },
{ code: 'function customWait (ms) { cy.wait(ms) }', parserOptions, errors },
{ code: 'const customWait = (ms) => { cy.wait(ms) }', 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 },
{ code: 'function customWait (ms = 1) { cy.wait(ms) }', parserOptions, errors },
{ code: 'const customWait = (ms = 1) => { cy.wait(ms) }', parserOptions, errors },
],
})

0 comments on commit 475a26c

Please sign in to comment.