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

More Playwright tasks

Back to the full Playwright cheat sheet