Cypress: custom timeout in specific should command

Viewed 8143

I need to have custom timeout in a specific should command in cypress. I have this json file which has global timeout:

{
  "viewportWidth": 1600,
  "defaultCommandTimeout": 10000
}

There is a specific case that I need a higher timeout, I would like something like this:

cy.get('body').should('contain','success', {timeout: 30000})

how do I do this? BTW, I do not want to override default command timeout, I need a specific timeout.

2 Answers

tl;dr

Just pass the timeout to get, it will pass it down to should.

cy.get('body', {timeout: 30000}).should('contain','success')

Explanation

This is explained in should's official documentation in the Timeouts section:

.should() will continue to retry its specified assertions until it times out.

cy.get('input', { timeout: 10000 }).should('have.value', '10')
// timeout here will be passed down to the '.should()'
// and it will retry for up to 10 secs

The technique is explained in greater detail in the docs about timeouts.

Related