diff --git a/lib/rules/no-unnecessary-waiting.js b/lib/rules/no-unnecessary-waiting.js index 405f1f8e..65ddb8ac 100644 --- a/lib/rules/no-unnecessary-waiting.js +++ b/lib/rules/no-unnecessary-waiting.js @@ -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' } diff --git a/tests/lib/rules/no-unnecessary-waiting.js b/tests/lib/rules/no-unnecessary-waiting.js index 50d1bca9..d1499aa6 100644 --- a/tests/lib/rules/no-unnecessary-waiting.js +++ b/tests/lib/rules/no-unnecessary-waiting.js @@ -20,6 +20,10 @@ 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: [ @@ -27,5 +31,7 @@ ruleTester.run('no-unnecessary-waiting', rule, { { 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 }, ], })