Fixtures in Cypress vs Playwright
A Playwright fixture is reproduced in Cypress with hooks, aliases and cy.session, but only partly. Setup goes in beforeEach or a custom command, the value is passed through an .as() alias, and cy.session caches the login. Teardown is not guaranteed, because an after hook may not run if the run is interrupted, so state is reset at the start of the test. There is no worker scope.
Cypress · Patterns and data
In Playwright a test receives an order as a parameter and it cleans itself up. How do you do that in Cypress?
A team moves from Playwright to Cypress (or back) and ports a test that needs an order. In Playwright it arrives as the order parameter and is removed after the test by itself. Cypress has no such parameter. This article does not repeat what a fixture file is. It answers a practical question: how to reproduce the behaviour of Playwright fixtures with Cypress tools and where it cannot be reproduced.
How it works and why. A Playwright fixture has three parts: setup, a value, teardown. The runner calls it before a test that asked for it and cleans up after it, even when the test failed. Cypress splits this role into separate mechanisms: setup in hooks and commands, passing the value through an alias, and cleanup is left to you. The difference is fundamental: Playwright guarantees teardown, while Cypress does not guarantee that an after hook runs at all (for example when the run is interrupted).
| Playwright | Cypress |
|---|---|
The order parameter | Alias .as('order') and this.order or cy.get('@order') |
Setup before use() | beforeEach or a custom command |
Teardown after use() | Cleanup before a test; after has no guarantee |
The worker scope | None: before applies within a spec, cy.session caches login |
auto: true | A global beforeEach in support/e2e.ts |
| JSON data: a plain import | cy.fixture() or a JSON import |
Cleanup: who is responsible for it
Teardown is guaranteed
Code after await use(order) runs even when the test failed. The runner does the cleanup.
Cleanup before the test
Every test starts from a known state. An after hook may not run, so nothing relies on it.
Example: the same order with two tools. Assumptions: the app has POST /api/orders, DELETE /api/orders/:id and a test-only reset endpoint POST /api/test/reset. Playwright:
import { test as base, expect } from '@playwright/test';
export const test = base.extend<{ order: { id: number } }>({
order: async ({ request }, use) => {
const res = await request.post('/api/orders', { data: { product: 'Book' } });
const order = await res.json();
await use(order);
await request.delete('/api/orders/' + order.id);
},
});
test('shows the order', async ({ page, order }) => {
await page.goto('/orders/' + order.id);
await expect(page.getByTestId('status')).toHaveText('new');
});
Cypress:
beforeEach(() => {
cy.request('POST', '/api/test/reset'); // cleanup at the start
cy.login('anna@example.com', 'secret');
cy.request('POST', '/api/orders', { product: 'Book' })
.its('body')
.as('order');
});
it('shows the order', function () {
cy.visit('/orders/' + this.order.id);
cy.get('[data-cy=status]').should('have.text', 'new');
});
Why this way. Cypress clears browser cookies and storage between tests (test isolation is on by default), but server data stays. So a reset at the start is more reliable than deletion at the end: it works even after an abruptly interrupted run. The function () {} form is needed for this.order to work. With an alias created in beforeEach, the test receives the value as a Playwright test does with a parameter, although the alias is not type-checked.
Limits and alternatives. Not reproduced: the worker scope, automatic ordering of dependencies between fixtures, a typed parameter, guaranteed teardown. For reuse, custom commands and cy.session help, and for types you can wrap the fetching in a command with a declared Chainable. This is not "worse", it is a different model: Cypress keeps a test linear and relies on resetting state rather than on a hierarchy of dependencies. For data that needs a network path, a fixture stays a file: cy.fixture or { fixture } in cy.intercept.
Common mistakes.
- Cleaning data only in
after/afterEach: after a broken run the next one starts with dirty state. - Looking for a "fixture parameter" in Cypress and writing your own wrapper: an alias is simpler.
- Relying on spec order, because
beforeis not shared between files. - Calling a JSON file "a fixture as in Playwright" in an interview.
Check yourself
A Cypress test creates an order in beforeEach and deletes it in afterEach. The run was interrupted mid-test. What happens on the next run?
Answer
The order stays in the system because afterEach did not run. If tests assume an empty state they may fail. Resetting data at the start of each test is more reliable.
Why is the Cypress test in the example written as function () {} and not an arrow?
Answer
The alias is available as this.order, and an arrow function has no this of its own. The alternative: an arrow and cy.get('@order').