samedi 23 janvier 2021

Chaining functions in Cypress

Context

I am having trouble trying to understand when asynchrony in Cypress should be handled by the developer and when not (because it is handled under the hood).

Consider these 2 tests:

A)

it('stackoverflow example',() => {
    cy.get('#soSection').should('contain', 'Stack Overflow').then(() =>{
      cy.get('#soButton').click().then(() => {
        cy.get('#soSection').should('not.contain', 'Stack Overflow');
      });
    });
});

B)

it('stackoverflow example',() => {
  cy.get('#soSection').should('contain', 'Stack Overflow');
  cy.get('#soButton').click();
  cy.get('#soSection').should('not.contain', 'Stack Overflow');
});
  • #soSection will contain 'Stack Overflow' until we click the button. Therefore, both lines should run someway synchronously.
  • (A) uses then which enables you to work with the subject yielded from the previous command. (B) does not use it.

What I've tried so far

I've run both (A) and (B) tests. Both methods work well and behave the same way.


Doubts

  1. If we needed to use the previous subject then we should only use the method (A) with chainables. Are there any other cases where we MUST use chainables like in (A) example?
  2. Look at the 3 lines of code in (B). We expect those 3 lines run in that order to success. However, Cypress claim to run asynchronously. Therefore, how can we be sure those 3 lines will run in the order expected?

Aucun commentaire:

Enregistrer un commentaire