What are fixtures in Cypress? Are they the same as in Playwright?
In Cypress a fixture is a data file, while in Playwright it is a function with setup and teardown: the word is the same, the meaning differs. The cy.fixture() command reads a file from cypress/fixtures and has no lifecycle. Playwright prepares a resource before await use() and cleans up after it. In Cypress hooks, custom commands and cy.session play that role, and cleanup is better done before the test.
Cypress · Patterns and data
A job post asks for "fixtures experience". Which kind: Cypress or Playwright?
A Playwright candidate talks about fixtures: scopes, dependencies, teardown. A Cypress interviewer asked about a JSON file with a test user. Both used the same word and did not understand each other. This lesson separates the two meanings, shows how each works and what plays the role of Playwright fixtures in Cypress.
How it works and why. In Cypress a fixture is a data file in the cypress/fixtures folder (the path is set by the fixturesFolder option). cy.fixture() reads it, and the .json extension can be omitted. A fixture has no lifecycle: it prepares and cleans up nothing. In Playwright a fixture is a function with setup and teardown: code before await use(value) prepares a resource, code after it cleans up, and the test receives the value as a parameter. So one word means different things: "data" versus "a dependency with a lifecycle".
One word, two things
A data file
cypress/fixtures/*.json. Read by cy.fixture() or used as a response in intercept. Cleans up nothing.
A dependency with a lifecycle
test.extend(): setup, await use(), teardown. Test and worker scopes, dependencies between fixtures.
Example: data from a file. The file cypress/fixtures/users/admin.json contains { "email": "admin@example.com" }. The test uses it two ways: in a form field and as a network response.
cy.fixture('users/admin').then((admin) => {
cy.get('[data-cy=email]').type(admin.email);
});
// an empty response instead of a call to the server
cy.intercept('GET', '/api/stats', { fixture: 'stats-empty.json' });
cy.visit('/statistics');
cy.get('[data-cy=empty-state]').should('be.visible');
// alias: this.admin works in function(), not in an arrow
beforeEach(() => cy.fixture('users/admin').as('admin'));
it('uses the alias', function () {
cy.get('[data-cy=email]').type(this.admin.email);
});
Why this way. A fixture with cy.intercept gives a predictable response: the "empty report" state is reproduced without preparing the database. Cypress caches a fixture’s contents, but every cy.fixture call yields a copy, so mutating the returned object does not affect later calls. Editing the file itself during the run is not picked up, though: the cached contents apply. An alternative: import admin from '../fixtures/users/admin.json' with resolveJsonModule in tsconfig, where the data is typed instead of loaded by a command.
What replaces Playwright fixtures in Cypress. Closer analogues: beforeEach/before for setup, custom commands for repeated actions, cy.session for a logged-in state and .as() aliases for passing values. There is no worker scope, dependencies between "fixtures" are not declared, and in Cypress cleanup is better done before a test: an after hook does not run if the run is interrupted.
Common mistakes.
- Calling
cy.fixture"like in Playwright": in an interview this shows mixed-up terms. - Putting real passwords or tokens in the JSON: the file lands in the repository.
- Using
this.adminin an arrow function:thisis empty, you needfunction () {}. - Counting on
afterto remove data: after an interrupted run it stays.
Check yourself
A test with cy.get(...).type(this.admin.email) fails with "Cannot read properties of undefined". The alias is created in beforeEach. What is wrong?
Answer
Most likely the test is an arrow function: it has no this of its own, so the alias is unavailable. Use it(..., function () { ... }) or read the value with cy.get('@admin').
Which part of a Playwright fixture (setup, value, teardown) does cy.fixture not give at all?
Answer
Setup and teardown: cy.fixture only returns data from a file. Cypress does setup and cleanup with hooks, commands and cy.session.