I have alias on my fixture stub, so this is working for me:
describe("some page", () => {
beforeEach(() => {
cy.intercept("/users", {
fixture: users.json,
});
cy.visit("/somewhere");
});
it("show something", () => {
cy.wait("@firstApiCall").then(() => {
cy.wait("@2ndApiCall").then(() => {
cy.get("test:something").should("exist");
});
});
});
});
but this won't work?
it("show something", () => {
await cy.wait("@firstApiCall");
await cy.wait("@firstApiCall");
cy.get("test:something").should("exist");
});
Also, how can I avoid repeating cy.wait(apiCall) in each it block?
It probably fails with:
Unexpected reserved word 'await'.
and that's because await keyword could be used only in async functions, your callback function is not async.
You might write:
it("show something", async () => {
await cy.wait("@firstApiCall");
await cy.wait("@firstApiCall");
cy.get("test:something").should("exist");
});
But I wonder why do you want to do it? Cypress does a good job running the commands in the order in which they are written (https://docs.cypress.io/guides/core-concepts/introduction-to-cypress#Commands-Run-Serially).
I also doubt your example actually works:
describe("some page", () => {
beforeEach(() => {
cy.intercept("/users", {
fixture: users.json,
});
cy.visit("/somewhere");
});
it("show something", () => {
cy.wait("@firstApiCall").then(() => {
cy.wait("@2ndApiCall").then(() => {
cy.get("test:something").should("exist");
});
});
});
});
You don't alias firstApiCall and 2ndApiCall, so Cypress doesn't know what to wait for.