The three layers
Each layer trades speed for realism. Unit tests run in milliseconds but prove little about how the system fits together. End-to-end tests exercise the real thing but are slower and have more reasons to fail. A healthy suite uses the cheapest layer that can catch a given bug.
- L1 End-to-endPlaywrightReal browserStagingA small set of critical journeys: sign in, checkout, submit a form.
- L2 Integration and APIPlaywright requestHTTP contractsTest databaseServices and data working together, without a browser.
- L3 UnitVitest or JestPure functionsComponentsBusiness rules and edge cases, fast enough to run on every save.
Decide by asking what could break
Start from the failure you want to catch, not from the feature.
- A price is calculated wrongly: that is logic. Write a unit test and cover twenty input combinations in milliseconds.
- The API accepts an invalid order: that is a contract. Write an API test.
- A cookie banner covers the submit button: only a real browser will notice. Write an end-to-end test.
A rule of thumb helps with the grey areas: push each check as low as it can go while it can still fail for the right reason. When a bug escapes, ask which layer should have caught it, and add the test there rather than at the top by default.
API tests without a browser
Playwright's request fixture sends HTTP calls directly, using the baseURL from your configuration. These tests are quick and cover validation and error handling far more cheaply than driving a form.
import { test, expect } from '@playwright/test';
test('rejects an order with no items', async ({ request }) => {
const res = await request.post('/api/orders', {
data: { customerId: 'c_123', items: [] },
});
expect(res.status()).toBe(422);
expect(await res.json()).toMatchObject({ error: 'items_required' });
});
One end-to-end test that earns its place
Locate elements the way people do, by role and accessible name. These locators survive CSS refactors and double as a basic accessibility check, because a control with no name cannot be found.
import { test, expect } from '@playwright/test';
test('visitor can send an enquiry', async ({ page }) => {
await page.goto('/contact');
await page.getByLabel('Full name').fill('Test Visitor');
await page.getByLabel('Email').fill('visitor@example.com');
await page.getByLabel('Message').fill('We need a new booking flow.');
await page.getByRole('button', { name: 'Send enquiry' }).click();
// Web-first assertion: retries until it passes or times out.
await expect(page.getByRole('status')).toContainText('Thank you');
});
Notice what this test does not do. It does not check every validation message or field combination, because lower layers cover those faster. It proves that a visitor can complete the journey in a real browser, with the real front end talking to a real backend.
Keeping the suite reliable
- Let Playwright wait. Web-first assertions such as toBeVisible retry automatically, so fixed sleeps are rarely needed.
- Give each test its own data. Tests that share an account fail in ways that depend on run order.
- Stub third parties with page.route, so an outage in a payment sandbox does not fail your build.
- Set trace to on-first-retry in the use section of your configuration, so a CI failure can be replayed step by step.
- Treat a flaky test as a defect. Fix it or remove it, because a suite people ignore protects nobody.