Skip to content

Commit

Permalink
fix: added validation for timeout for query operations
Browse files Browse the repository at this point in the history
  • Loading branch information
percytrar committed Apr 14, 2024
1 parent 79a267c commit e18f830
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<!-- See the ../guides/writing-the-cypress-changelog.md for details on writing the changelog. -->
## 13.7.4
_Released 4/21/2024 (PENDING)_

**Bugfixes**

- Fixed an issue where timeout throw was not happening when user provided invalid timeout value. Fixes [#29323](https://github.com/cypress-io/cypress/issues/29323).

## 13.7.3

_Released 4/11/2024_
Expand Down
58 changes: 58 additions & 0 deletions packages/driver/cypress/e2e/commands/querying/querying.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,64 @@ describe('src/cy/commands/querying', () => {
})
})

describe('should throw when timeout is not a number', () => {
it('timeout passed as plain object {}', (done) => {
cy.get('#some-el', { timeout: {} })
cy.on('fail', (err) => {
expect(err.message).to.eq('contains invalid timeout - [object Object]')
done()
})
})

it('timeout passed as some string', (done) => {
cy.get('#some-el', { timeout: 'abc' })
cy.on('fail', (err) => {
expect(err.message).to.eq('contains invalid timeout - abc')
done()
})
})

it('timeout passed as null', (done) => {
cy.get('#some-el', { timeout: null })
cy.on('fail', (err) => {
expect(err.message).to.eq('contains invalid timeout - null')
done()
})
})

it('timeout passed as NaN', (done) => {
cy.get('#some-el', { timeout: NaN })
cy.on('fail', (err) => {
expect(err.message).to.eq('contains invalid timeout - NaN')
done()
})
})

it('timeout passed as Boolean', (done) => {
cy.get('#some-el', { timeout: false })
cy.on('fail', (err) => {
expect(err.message).to.eq('contains invalid timeout - false')
done()
})
})

it('timeout passed as array', (done) => {
cy.get('#some-el', { timeout: [] })
cy.on('fail', (err) => {
expect(err.message).to.eq(`contains invalid timeout - ${[]}`)
done()
})
})
})

it('should timeout when element can\'t be found', (done) => {
cy.get('#some-el', { timeout: 100 })
cy.on('fail', (err) => {
expect(err.message).to.contain('Timed out retrying after 100ms')
done()
})
})

it('can increase the timeout', () => {
const missingEl = $('<div />', { id: 'missing-el' })

Expand Down
12 changes: 12 additions & 0 deletions packages/driver/src/cy/commands/querying/querying.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,21 @@ function getAlias (selector, log, cy) {
}

export default (Commands, Cypress, cy, state) => {
function validateTimeoutFromOpts (options: GetOptions | ContainsOptions | ShadowOptions = {}) {
if (_.isPlainObject(options) && options.hasOwnProperty('timeout') && !_.isFinite(options.timeout)) {
$errUtils.throwErr(new Error(`contains invalid timeout - ${options.timeout}`))
}
}

Commands.addQuery('get', function get (selector, userOptions: GetOptions = {}) {
if ((userOptions === null) || _.isArray(userOptions) || !_.isPlainObject(userOptions)) {
$errUtils.throwErrByPath('get.invalid_options', {
args: { options: userOptions },
})
}

validateTimeoutFromOpts(userOptions)

const log = userOptions._log || Cypress.log({
message: selector,
type: 'parent',
Expand Down Expand Up @@ -253,6 +261,8 @@ export default (Commands, Cypress, cy, state) => {
$errUtils.throwErrByPath('contains.empty_string')
}

validateTimeoutFromOpts(userOptions)

// find elements by the :cy-contains pseudo selector
// and any submit inputs with the attributeContainsWord selector
const selector = $dom.getContainsSelector(text, filter, { matchCase: true, ...userOptions })
Expand Down Expand Up @@ -361,6 +371,8 @@ export default (Commands, Cypress, cy, state) => {
consoleProps: () => ({}),
})

validateTimeoutFromOpts(userOptions)

this.set('timeout', userOptions.timeout)
this.set('onFail', (err) => {
switch (err.type) {
Expand Down

0 comments on commit e18f830

Please sign in to comment.