4
votes

Hello I have a form submit to a graph ql api. When I click the submit button I see that cypress cancels one of the requests to the api before it's able to get a response. Any idea how to prevent this? Edit: I've tried adding cy.wait(6000); or cy.wait('apiAlias'), post click submit, and neither help.

Text

1
It's probably your application that cancels the request. Otherwise, you can use cy.wait() to wait for response. - martin
Can you elaborate as to why do you think it's the application rather than cypress? As I mentioned when I manually click submit the request doesn't get canceled, only when run via cypress. I've tried adding cy.wait(6000); and cy.wait('apiAlias'), post click submit; neither help - HRVHackers
upon further digging it seems like this is being caused by a new feature of cypress where it will "cancel XHR's optimistically." is there a way to temporarily disable that feature upon a form submit? github.com/cypress-io/cypress/pull/3974 - HRVHackers

1 Answers

2
votes

Fixed this by preventing the default behavior of the form submit as suggested here

function preventFormSubmitDefault(selector) {
  cy.get(selector).then(form$ => {
    form$.on("submit", e => {
      e.preventDefault();
    });
  });
}

And then in the test:

    preventFormSubmitDefault("form");
    cy.get('[data-test="submitButton"]').click();