Test Hooks & Grouping in Playwright
From the Playwright cheat sheet ยท Writing Tests ยท verified Jul 2026
Test Hooks & Grouping
Organize tests with describe blocks and lifecycle hooks
typescript
import { test, expect } from '@playwright/test';
test.describe('Dashboard', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/dashboard');
});
test('shows welcome message', async ({ page }) => {
await expect(page.getByText('Welcome')).toBeVisible();
});
test('displays stats', async ({ page }) => {
await expect(page.getByTestId('stats')).toBeVisible();
});
});๐ก beforeEach/afterEach run for every test โ use for navigation and cleanup
โก beforeAll/afterAll run once per describe block โ ideal for database seeding
๐ Use describe.configure({ mode: "serial" }) when tests depend on each other
๐ข Describe blocks can be nested for better organization of large test suites
hooksdescribeorganize
Continue with Playwright
Save the full cheat sheet or work through every related task.