Playwright tests in a pnpm monorepo, kept close to the app

Put browser tests next to the app they cover, run them with a pnpm filter, and keep a small accessibility check in the same suite.

Browser tests go stale when they live in a folder nobody opens. In a pnpm monorepo I keep Playwright tests next to the app they actually cover, and I run them with a workspace filter so a change in the API does not pretend to run the public site tests.

This is a setup guide, not a tour of every Playwright feature.

Where the tests live

A shape that has worked for me:

apps/web/
  package.json
  tests/public.spec.ts
apps/api/
  package.json

The web package owns the browser tests. The root package may depend on `@playwright/test` so the version is shared, or the web package can own the dependency. Either is fine as long as there is one version and a single Playwright config that the web tests load.

A root script then calls the package:

pnpm --filter web test:public:browser

If I have a second surface, such as a form or an authenticated flow, it gets its own script. I would rather type a slightly longer command than run one giant suite that hides a failure in a scroll of unrelated work.

A first test that asserts something real

Start with a test that loads a public page and checks a heading or a landmark, not a screenshot of the whole homepage. Screenshots have their place. They are a poor first assertion because they fail for reasons that have nothing to do with the change you made.

import { test, expect } from '@playwright/test';

test('the journal index has a main heading', async ({ page }) => {
  await page.goto('/journal');
  await expect(page.getByRole('heading', { level: 1 })).toBeVisible();
});

Use roles and names when you can. That is closer to how someone actually finds the page, and it stays stable when a class name changes.

Accessibility checks belong in the same run

I also keep `@axe-core/playwright` in the browser suite. It will not catch every WCAG problem, and I do not treat a clean axe run as a certificate. It will catch a lot of missing names and contrast mistakes while the test is already opening the page.

A typical pattern is: visit the page, wait for a known landmark, then run `axe` against that page and fail the test if there are violations you have not explicitly allowed. Keep the allow-list tiny. If you start excluding whole rules because a template is messy, fix the template.

What I keep out of the laptop hook

A full browser suite is often too slow for `pre-push`. I run a unit test and a typecheck there, and I leave Playwright to CI and to a local command I run when I change the UI. If the suite is small and headless, you can revisit that. If people start skipping the hook, it has already failed.

The useful habit is the same as any other test: run it on a clean machine, against a known URL, and fail on an assertion a human can read. Playwright is just the driver.

Back to Journal