Fixtures give you a clean way to store and reuse static test data. This lesson covers loading fixtures with cy.fixture() and using them to stub network responses via cy.intercept().
What Is a Fixture?
A fixture is a static file, usually JSON, stored in cypress/fixtures/, representing a fixed, known piece of test data: a user object, a list of products, an API response shape. Fixtures make tests deterministic by decoupling them from whatever live data currently exists in a real backend.
The most common real-world use of fixtures is providing a canned response body for cy.intercept(), letting a test control exactly what the frontend receives without depending on a real backend being available or returning predictable data.
This test is now fully deterministic: exactly two products, with known names and prices, every single run.
Aliasing Fixtures with .as()
Aliasing a fixture makes it reusable across multiple steps of the same test without reloading it, and keeps intent clear when a fixture's data is referenced several times.
beforeEach(() => {
cy.fixture('user').as('userData');
});
it('pre-fills the profile form', function () {
cy.visit('/profile');
cy.get('[data-cy="name"]').should('have.value', this.userData.name);
});
Aliased fixtures are accessed via this.aliasName inside a function () {} test callback (not an arrow function, which does not bind this).
Common Mistakes
Using an arrow function for a test that needs this.aliasName access to an aliased fixture, arrow functions don't bind this.
Hardcoding fixture data directly in a spec file instead of a dedicated fixture file, making it harder to reuse and maintain.
Forgetting fixture paths are relative to cypress/fixtures/, not the spec file's own location.
Letting fixture files drift out of sync with the real API's actual response shape over time.
Key Takeaways
Fixtures are static files (commonly JSON) stored in cypress/fixtures/ for deterministic test data.
cy.fixture() loads and parses fixture data for direct use in a test.
Fixtures pair naturally with cy.intercept() to stub network responses.
Aliased fixtures need a function () {} callback, not an arrow function, to access this.aliasName.
Pro Tip
Keep fixture files structurally in sync with your real API by periodically diffing them against an actual response (or generating them from real, sanitized production-like data), stale fixtures are a subtle source of tests that pass against fake data but miss real regressions.
You now understand Cypress fixtures. Next, look at broader Test Data strategies beyond static fixture files.