Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/rules/no-unnecessary-waiting.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,17 @@ function isIdentifierNumberConstArgument(node, scope) {
// we don't know if WAIT is a number or alias '@someRequest', so don't fail
if (definition.type === 'ImportBinding') return false

const param = definition.node.params[definition.index]
// Get the identifier's parent node to check for a default value.
// For `function wait (amount = 1) { ... }`, `definition.name` is the `amount` Identifier,
// and its parent is the AssignmentPattern `amount = 1`.
// This also handles destructured parameters like `function wait ({ ms = 1 }) { ... }`.
const nameParent = definition.name.parent

// 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
if (!nameParent || nameParent.type !== 'AssignmentPattern') return false

// function wait (amount = 1) { cy.wait(amount) } or
// function wait (amount = '@alias') { cy.wait(amount) }
return typeof param.right.value === 'number'
return typeof nameParent.right.value === 'number'
}
17 changes: 17 additions & 0 deletions tests/lib/rules/no-unnecessary-waiting.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ ruleTester.run('no-unnecessary-waiting', rule, {
{ code: 'function customWait (ms) { cy.wait(ms) }' },
{ code: 'const customWait = (ms) => { cy.wait(ms) }' },

// Destructured parameters without numeric defaults
{ code: 'function customWait ({ ms }) { cy.wait(ms) }' },
{ code: 'function customWait ({ ms = "@someRequest" }) { cy.wait(ms) }' },
{ code: 'function customWait ([ms]) { cy.wait(ms) }' },
{ code: 'function customWait ([ms = "@someRequest"]) { cy.wait(ms) }' },
{ code: 'function customWait ({ opts: { ms = "@someRequest" } }) { cy.wait(ms) }' },

// Other definition types that should not be flagged
{ code: 'function f() {} cy.wait(f)' },
{ code: 'class C {} cy.wait(C)' },
{ code: 'try {} catch (e) { cy.wait(e) }' },

{ code: 'import BAR_BAZ from "bar-baz"; cy.wait(BAR_BAZ)' },
{ code: 'import { FOO_BAR } from "foo-bar"; cy.wait(FOO_BAR)' },
{ code: 'import * as wildcard from "wildcard"; cy.wait(wildcard.value)' },
Expand All @@ -43,5 +55,10 @@ ruleTester.run('no-unnecessary-waiting', rule, {
{ code: 'cy.get(".some-element").wait(10)', errors },
{ code: 'cy.get(".some-element").contains("foo").wait(10)', errors },
{ code: 'const customWait = (ms = 1) => { cy.get(".some-element").wait(ms) }', errors },

// Destructured parameters with numeric defaults
{ code: 'function customWait ({ ms = 1 }) { cy.wait(ms) }', errors },
{ code: 'function customWait ([ms = 1]) { cy.wait(ms) }', errors },
{ code: 'function customWait ({ opts: { ms = 1 } }) { cy.wait(ms) }', errors },
],
})
Loading