Any way to alias fixture and also use in intercept (see examples)? #20275
-
Hello. This is an edge case question, but wondering if anyone has a pattern that could make it work. I have multiple intercepts, which must be defined in order of specificity because of query params. Each one of them references a paramaterized fixture path. I also want to alias the fixtures for reference in tests. The order of intercepts is important, but I may have my example backwards, not sure. The point is some have to be defined inside Here's a simple example: // imagine fixtures have a nested array
beforeEach(() => {
// The order of intercept is important
cy.fixture(`my-${parameterized}-filename.json`)
.as("fixtureAlias")
.then(fixtureAlias => { cy.intercept("**/some/path?q1=foo", fixtureAlias) })
cy.fixture(`my-${parameterized2}-filename.json`)
.as("fixtureAlias2")
.then(fixtureAlias2 => { cy.intercept("**/some/path?q1=foo&q2=bar", fixtureAlias2) })
cy.intercept("**/some/path?q1=foo&q2=bar&q3=baz", { fixture: "my-simple-filename.json" } )
});
it("is a test", function() {
cy.get("thing").should("have.length", this.fixtureAlias.nestedArray.length);
cy.get("thing2").should("have.length", this.fixtureAlias2.nestedArray.length);
}); This is failing somewhere for me, whereas this works: // imagine fixtures have a nested array
beforeEach(() => {
cy.fixture(`my-${parameterized}-filename.json`).as("fixtureAlias");
cy.fixture(`my-${parameterized2}-filename.json`).as("fixtureAlias2");
// The order of intercept is important
cy.intercept("**/some/path?q1=foo", `my-${parameterized}-filename.json`);
cy.intercept("**/some/path?q1=foo&q2=bar", `my-${parameterized2}-filename.json`);
cy.intercept("**/some/path?q1=foo&q2=bar&q3=baz", { fixture: "my-simple-filename.json" } )
});
it("is a test", function() {
cy.get("thing").should("have.length", this.fixtureAlias.nestedArray.length);
cy.get("thing2").should("have.length", this.fixtureAlias2.nestedArray.length);
}); I don't know if there's really much or any overhead defining each fixture twice, but is there any way to get something like the first pattern to work? Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I just realized there's a way around this aliasing the beforeEach(() => {
// The order of intercept is important
cy.intercept("**/some/path?q1=foo", { fixture: `my-${parameterized}-filename.json` }).as("interceptAlias1");
cy.intercept("**/some/path?q1=foo&q2=bar", { fixture: `my-${parameterized2}-filename.json` }).as("interceptAlias2");
cy.intercept("**/some/path?q1=foo&q2=bar&q3=baz", { fixture: "my-simple-filename.json" } )
});
it("is a test", function() {
cy.wait("@interceptAlias1").then(interception => {
cy.get("thing").should("have.length", interception.response.body.nestedArray.length);
});
cy.wait("@interceptAlias2").then(interception => {
cy.get("thing2").should("have.length", interception.response.body.nestedArray.length);
});
}); |
Beta Was this translation helpful? Give feedback.
I just realized there's a way around this aliasing the
intercept
s and usingcy.wait
: