Search code examples
cypresstimeout

Why isnt cypress honoring my timeout for a single command?


I'm a couple of weeks into cypress test automation (cypress V12) and while it seems cool I am confused by what I am seeing.

I have code that looks like this:

    it('shows the problem for Stack Overflow', {defaultCommandTimeout: 20000}, function () {
      //bunch of commands that do stuff are here but I dont think they are related to the issue...

      const loadingSpinnerLocator = ".k-loader-canvas";
      cy.get(loadingSpinnerLocator).should('be.visible');
      cy.get(loadingSpinnerLocator).should('not.exist' ,{ timeout: 15000 });          

    })

this code works fine because I am doing a workaround in the top line and overriding the defaultCommandTimeout to 20 seconds.

If I remove the override on the "it statement" the test fails when we are making sure that the spinner is gone after 4 seconds. Isn't my code saying to look for 15 seconds???

How do I tell cypress to "wait up to 15 seconds for the loading spinner to not be there" without bumping up the timeout for the whole test??


Solution

  • Generally speaking, { timeout: 15000 } is an options object.

    All Cypress commands are documented with a Syntax section which tells you if you can pass options, for example .should()

    Syntax

    .should(chainers)
    .should(chainers, value)
    .should(chainers, method, value)
    .should(callbackFn)
    

    This tells you there is no options syntax for .should().

    On the other hand for .get(), there are permutations that allow an options parameter,

    Syntax

    cy.get(selector)
    cy.get(alias)
    cy.get(selector, options)
    cy.get(alias, options)