Ahm23
Ahm23

Reputation: 366

Set timeout for Cypress expect assertion

My issue is that I'd like to have a timeout on the expect assertion from Cypress's library but can't seem to figure out a method to do so.

I've tried greatly increasing the global timeout in cypress.conf; however, that didn't work.

if ($widget.find('report-widget')) {
  SVGCount++;
  expect(barGraphsChange[index]).to.not.equal(undefined)
  console.log(barGraphsChange, index)
  cy.getBarGraphTotal(SVGCount, barGraphsChange[index])
}

If not a timeout then a reasonable workaround would be nice as well, Thanks!

Also a note: barGraphsChange[index] is in the process of getting calculated and assigned from a called custom Cypress command earlier on during this phase.

Upvotes: 9

Views: 11096

Answers (1)

Vangelisz Ketipisz
Vangelisz Ketipisz

Reputation: 977

You can customise your expectations with should(), and this will automatically retry your assertions for either the specified timeout, or the global defaultCommandTimeout if none provided. In your case I could probably imagine wrapping your object, specify a timeout and then passing your logic to should:

cy.wrap(someFunction, {timeout: 25000}).should(someFunction => expect(someFunction()).to.eq(someValue))

Read more here: https://docs.cypress.io/api/commands/should.html#Function

Upvotes: 9

Related Questions