cypress intercept not overriding cypress alias

Mario Garcia

I'm doing some testing and I intercept some api calls to the same url, I do one beforeEach, and then another one on the test, but for some reason I does not understand that I changed the alias. I was doing some reading, and the overriding was fixed, but apparently is not? Please feel free to ask more questions. Hope I can get some input.

My code:

// Describe block:

  beforeEach(() => {
    cy.visit("/");
    cy.intercept(
      {
        method: "GET",
        url: "/customers*",
        hostname: "local.api",
      },
      {
        fixture: "customers.json",
      }
    ).as("customers");
    cy.get("[class^=ant-menu-item]", { multiple: true }).eq(1).click();
    cy.wait("@customers");
  });



  [
    ["customerName", "ASC", 0],
    ["nextReviewDate", "ASC", 1],
    ["nextReviewType", "ASC", 2],
  ].forEach(([sortValue, sortOrder, index]) => {
    it(`Sort by direction ${sortOrder} order ${sortValue}`, () => {
      cy.get(".ant-table-column-sorters", { multiple: true }).eq(index).click();
      cy.intercept("GET", "/customers*").as("request");
      cy.wait("@request").then((interception) => {
        cy.wrap(interception.response.statusCode).should("eq", 200);

        cy.wrap(interception.request.url).should(
          "include",
          `https://allica.local.api/customers?page=1&sortBy=${sortValue}&pageSize=5&direction=${sortOrder}`
        );
      });
    });
  });

The following error:

enter image description here

If there is not overriding, how can overcome this test?

Thanks in advance.

Fody

The intercept is an event listener. It must be set up before the event is triggered

cy.intercept("GET", "/customers*").as("request");
cy.get(".ant-table-column-sorters", { multiple: true }).eq(index).click();
cy.wait("@request").then((interception) => {
  ...

Actually, there's no change to the intercept between tests so you can just set it once and wait multiple times

before(() => cy.intercept("GET", "/customers*").as("request"))

[
  ["customerName", "ASC", 0],
  ["nextReviewDate", "ASC", 1],
  ["nextReviewType", "ASC", 2],
].forEach(([sortValue, sortOrder, index]) => {
  it(`Sort by direction ${sortOrder} order ${sortValue}`, () => {
    cy.get(".ant-table-column-sorters").eq(index).click();
    cy.wait("@request").then((interception) => {

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related