Cypress · Browser testing end to end
How do fixtures work in Cypress?
Short answer
Fixtures load external test data from cypress/fixtures. Use cy.fixture to read a file, and alias it or pass it to cy.intercept so tests stay readable and data is reused.
A fixture is a file, usually JSON, that holds test data. Cypress resolves the path relative to cypress/fixtures by default, so cy.fixture with users.json reads cypress/fixtures/users.json. Fixtures keep large payloads out of the test body and make the data easy to review.
describe("user list", () => {
beforeEach(() => {
cy.fixture("users.json").as("users");
cy.intercept("GET", "/api/users", { fixture: "users.json" }).as("getUsers");
cy.visit("/users");
cy.wait("@getUsers");
});
it("renders every fixture user", () => {
cy.get("@users").then((users) => {
cy.get("[data-cy=user-row]").should("have.length", users.length);
});
});
});Ways to use fixtures
- cy.fixture reads a file and yields its parsed contents
- A fixture option on cy.intercept stubs the response body
- Alias a fixture to reuse it across assertions in the same test
- Use .then to transform or parameterise fixture data before using it
- Keep fixtures small and purposeful instead of copying a production dump
Fixtures are static. If a test needs a unique value, generate it in the test or in a custom command rather than editing the file. For database-backed tests, seed through cy.task or a request, because a fixture only controls what the browser receives.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Where does cy.fixture look for files?
- 2How do you stub an intercept with a fixture?
- 3What is the difference between a fixture and a database seed?
- 4How do you use a fixture value inside an assertion?
Say it in the interview
Fixtures are files under cypress/fixtures that hold test data. I load them with cy.fixture or the fixture shorthand on an intercept, and I alias them when several assertions need the same data. I keep fixtures small and focused. If the test needs the backend to change, I seed through a task or a request, because a fixture only controls the browser's view of the response.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Cypress mock interview and get scored feedback on depth, clarity and confidence.