0
votes

I'm tying to set up some basic testing in cypress, stubbing some basic http calls using cy.server() cy.route() and cy.wait(). In my app, I need to assert on the number of times that the route has been stubbed. For instance, when I submit a form it send a request to localhost:3001, if I submit the form again it will submit again. I want to assert on the number of submissions. Something like expect(xhr).to.have.lenght(2), or something similar, but I can't figure out how.

This is my simple test so far

it("Checking number of requests", () => {
    cy.server();
    cy.route({
      method: "POST",
      url: "**/signupNewUser*",
      response: {
        kind: "identitytoolkit#SignupNewUserResponse",
        idToken: "sarasa",
        email: "[email protected]",
        refreshToken: "sarasa2",
        expiresIn: "3600",
        localId: "sarasa3",
        customTestingProperty: "custom"
      }
    }).as("postAPI");

    cy.fillForm(password);
    cy.contains("Submit").click();
    cy.contains("Submit").click();

    // Here I would like to assert that "**/signupNewUser*" has been requested 2 times. 
// I don't want to test for how many times has the button been clicked or the form been submitted.

  });
1

1 Answers

0
votes

Based on the GitHub thread on Cypress source - https://github.com/cypress-io/cypress/issues/477#issuecomment-412617742

There actually is an undocumented way to check the number of times an XHR was responded to using .all on the alias.

cy.wait('@postAPI')
cy.tick(10000)
cy.get('@postAPI.all').should('have.length', 2)