Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

187
Views
Is there a better way to assert that all intercepted requests are successful in Cypress?

I'm trying to test that all 12 requests involved in creating an object are successful, so far this is the only way I can figure out that works. IS there a better/more efficient/more flexible way that won't break as soon as we add or remove a request?

              cy.intercept('/api/**/**').as('objectCreation');
              $button.click();

              cy.wait('@objectCreation').its('response.statusCode').should('eq', 200);
              cy.wait('@objectCreation').its('response.statusCode').should('eq', 200);
              cy.wait('@objectCreation').its('response.statusCode').should('eq', 200);
              cy.wait('@objectCreation').its('response.statusCode').should('eq', 200);
              cy.wait('@objectCreation').its('response.statusCode').should('eq', 200);
              cy.wait('@objectCreation').its('response.statusCode').should('eq', 200);
              cy.wait('@objectCreation').its('response.statusCode').should('eq', 200);
              cy.wait('@objectCreation').its('response.statusCode').should('eq', 200);
              cy.wait('@objectCreation').its('response.statusCode').should('eq', 200);
              cy.wait('@objectCreation').its('response.statusCode').should('eq', 200);
              cy.wait('@objectCreation').its('response.statusCode').should('eq', 200);
              cy.wait('@objectCreation').its('response.statusCode').should('eq', 200);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I'll like more details about your question:

  • Are the requests identical?

However, to answer your question on how to simplify your shared, code snippet...you could create a variable representing the request creation attempts & keep calling the cy.intercept() method & asserting its response until you hit the specified request creation amount like so:

const reqObjCreationNum = 0; // reqObjCreationNum object creation attempts number

/**
 * @param {number} reqObjCreationNum - number of object creation attempts
 */
const createObject = ({ reqObjCreationNum }) => {
  cy.intercept('/api/**/**').as('objectCreation');

  while (reqObjCreationNum <= 12) {
    cy.wait('@objectCreation')
      .its('response.statusCode')
      .should('eq', 200);

    reqObjCreationNum += 1;
  }
};

createObject({ reqObjCreationNum });

I would suggest the DRY programming concept as a future reading.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!