Yes, but how should I address this problem? My req...
# help
m
Yes, but how should I address this problem? My requests are triggered once the page loads, and I want to check the paremeters it sent to our backend.
w
If the things are truly linked (eg it matters that we check both for the same page load), personally I'd do the
cy.visit
and both assertions all in the same
it
block, with no
before
. If I wanted to keep them isolated like you have them, I'd make that
before
a
beforeEach
. Or, since it's short, intercept the different things and visit at the start of each test. Another option is to not use aliases, declare some
let
variables before your
before
block,
cy.wait
for the interceptions in the
before
and assign the results to the variables. Then those variables are in scope for your later tests.
m
Thanks for the solutions! I'll probably go with the first one but keeping the before block. I still want to check how some components are being displayed after asserting the request paramters, so I don't think I should load the page 3 or 4 times to do that. As I'm checking the aliases in the first "it" block, they have not been cleared yet, so it works fine.
Copy code
javascript
describe('Testes', () => {
  before(() => {
    cy.loginAzure();

    cy.intercept(
      { method: "GET", url: "URL1" },
      { fixture: "FIXTURE1" }
    ).as("ALIAS1");

    cy.intercept(
      { method: "GET", url: "URL2" },
      { fixture: "FIXTURE2" }
    ).as("ALIAS2");

    cy.visit("/PAGE");
  });

  describe("checking request paramaters", () => {
    it("the three requests should be called with the correct paramaters", () => {
      const parametrosAnoMes = {
        // some parameters
      };
      const parametrosAnoMesCorredor = {
        // some parameters
      };

      cy.wait("@ALIAS1").then(({ request }) => {
        cy.decodeQueryParams(request.url).should('deep.equal', parametrosAnoMesCorredor);
      });

      cy.wait("@ALIAS2").then(({ request }) => {
        cy.decodeQueryParams(request.url).should('deep.equal', parametrosAnoMes);
      });
    })
  })

  describe("component test", () => {
    // Some components checks
  })
});
What do you think? Is it an ok solution or is it a bad practice to rely on the first "it" block to make those assertions?
w
Your call. I think it could be confusing to rely on this behavior, if somebody comes along and makes a different block first, it would hard to spot why a test suddenly started to fail. You could maybe nest describe blocks instead - one visit just for checking the network calls, and a fresh visit for everything else. Usually visits themselves don't seem that high-cost to me. Some people prefer fresh visits so the tests are more isolated, so I've see a ton of
cy.visit
in
beforeEach
callbacks. It's especially a good idea if your tests do any interactions. But either way, you have options now
m
Yeah, it could be a little confusing. I like the idea of one describe for network calls and another one for the components, I will do that.
Thank you very much!
5 Views