The problem with beforeEach
The first Playwright suite I inherited was 340 tests long and every one of them began with the same nineteen lines of setup. Auth token, tenant seeding, a clock stub, a feature flag, an uploaded fixture file. Copy-pasted, slightly different in each file, and impossible to reason about.
When a test failed, the first question was never "what broke?" but "what was different about this file's setup?". That question is the exact opposite of what test code should be asking.
- Setup lived in the test file, so it was duplicated and drifted
- Teardown order was implicit — whichever file happened to clean up last won
- A new engineer had to read 19 test files to learn the login flow
Fixtures push setup to the dependency graph
A fixture is not a helper function. It is a declaration that a piece of state exists for the scope of a test, with a defined teardown. The runner builds the dependency graph, runs setup in dependency order, and unwinds teardown in exactly the reverse.
Once the tenant is a fixture rather than a line of code, the test body reads like the story it is actually testing. And because the dependency graph is explicit, teardown order stops being tribal knowledge.
import { test as base, expect } from '@playwright/test';
type Tenant = { id: string; authToken: string; featureFlags: string[] };
// A fixture that owns its own lifecycle. Playwright resolves the
// dependency graph: `user` depends on `tenant`, so `tenant` is set up
// first and torn down last.
export const test = base.extend<{ tenant: Tenant }>({
tenant: async ({ request }, use) => {
const res = await request.post('/api/test-tenants', {
data: { plan: 'enterprise', region: 'eu-west' },
});
expect(res.ok()).toBeTruthy();
const tenant = (await res.json()) as Tenant;
await use(tenant);
// Teardown runs in reverse dependency order, so children are already
// gone by the time we delete the tenant.
await request.delete(`/api/test-tenants/${tenant.id}`);
},
});Scope is the design decision
The mistake I made early was making everything worker-scoped because it was faster in the short run. Worker-scoped means one browser for the whole shard, one tenant for hundreds of tests, and state bleeding between tests you did not think about.
The rule I landed on: a fixture should be as wide as its lifetime, and no wider. If the state is needed for the whole worker, it is worker-scoped. If it is needed for a single test, it is test-scoped. If it is needed for a describe block, it is a describe-scoped fixture.
import { test, expect } from '../fixtures/tenant';
// `test` inherits the tenant fixture from the base, and each test gets
// its own — so tests never see each other's seeded data.
test.describe('Checkout', () => {
test('completes a booking', async ({ page, tenant }) => {
await page.goto('/booking/new');
await page.getByTestId('pickup').fill(tenant.id);
await page.getByRole('button', { name: 'Confirm' }).click();
await expect(page.getByRole('heading', { name: 'Booked' })).toBeVisible();
});
test('rejects an expired promo code', async ({ page, tenant }) => {
await page.goto('/booking/new');
await page.getByTestId('promo').fill('EXPIRED');
await page.getByRole('button', { name: 'Apply' }).click();
await expect(page.getByText('This code has expired')).toBeVisible();
});
});Make the fixture read like the assertion
A fixture whose name matches what it guarantees is self-documenting. When I see `authenticatedUser` in a test signature, I know the request is already authorised and I do not need to read the fixture file. That single convention removed most of my onboarding questions.
The payoff shows up in maintenance rather than in the first week. When authentication changes, there is exactly one fixture to update, and every test that depends on it picks the change up automatically.
Key Takeaways
- A fixture is a lifecycle declaration, not a helper — the runner owns setup order and reverse-order teardown.
- Match fixture scope to actual lifetime: test, describe, or worker — never wider than needed.
- Name fixtures after the guarantee they provide, so the test signature documents itself.
- One fixture to change when auth or tenant logic changes — that is where the maintenance win lives.