Skip to content

Commit 475a26c

Browse files
committed
fix: Handle cy.wait in a function with a default parameter
1 parent e66bdfd commit 475a26c

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

lib/rules/no-unnecessary-waiting.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,27 @@ function isNumberArgument (node) {
4848
}
4949

5050
function isIdentifierNumberConstArgument (node, scope) {
51-
if (node.arguments[0].type !== 'Identifier') {
52-
return false
53-
}
51+
if (node.arguments[0].type !== 'Identifier') return false
5452

5553
const resolvedIdentifier = scope.resolve(node.arguments[0]).resolved
56-
const IdentifierValue = resolvedIdentifier.defs[0].node.init.value
54+
const definition = resolvedIdentifier.defs[0]
55+
const isVariable = definition.type === 'Variable'
56+
57+
// const amount = 1000 or const amount = '@alias'
58+
// cy.wait(amount)
59+
if (isVariable) {
60+
if (!definition.node.init) return false
61+
62+
return typeof definition.node.init.value === 'number'
63+
}
64+
65+
const param = definition.node.params[definition.index]
66+
67+
// function wait (amount) { cy.wait(amount) }
68+
// we can't know the type of value, so don't fail
69+
if (!param || param.type !== 'AssignmentPattern') return false
5770

58-
return typeof IdentifierValue === 'number'
71+
// function wait (amount = 1) { cy.wait(amount) } or
72+
// function wait (amount = '@alias') { cy.wait(amount) }
73+
return typeof param.right.value === 'number'
5974
}

tests/lib/rules/no-unnecessary-waiting.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,18 @@ ruleTester.run('no-unnecessary-waiting', rule, {
2020
{ code: 'cy.tick(500)', parserOptions },
2121

2222
{ code: 'const someRequest="@someRequest"; cy.wait(someRequest)', parserOptions, errors },
23+
{ code: 'function customWait (alias = "@someRequest") { cy.wait(alias) }', parserOptions, errors },
24+
{ code: 'const customWait = (alias = "@someRequest") => { cy.wait(alias) }', parserOptions, errors },
25+
{ code: 'function customWait (ms) { cy.wait(ms) }', parserOptions, errors },
26+
{ code: 'const customWait = (ms) => { cy.wait(ms) }', parserOptions, errors },
2327
],
2428

2529
invalid: [
2630
{ code: 'cy.wait(0)', parserOptions, errors },
2731
{ code: 'cy.wait(100)', parserOptions, errors },
2832
{ code: 'cy.wait(5000)', parserOptions, errors },
2933
{ code: 'const someNumber=500; cy.wait(someNumber)', parserOptions, errors },
34+
{ code: 'function customWait (ms = 1) { cy.wait(ms) }', parserOptions, errors },
35+
{ code: 'const customWait = (ms = 1) => { cy.wait(ms) }', parserOptions, errors },
3036
],
3137
})

0 commit comments

Comments
 (0)