playwright-component-testing
Installation
SKILL.md
Component Testing with Playwright
Test components with regular Playwright e2e tests against a small story gallery page hosted by the app's own dev server. No extra test runner, bundler integration or npm packages are required.
Concept
- A story is a tiny wrapper component that embeds the component under test in one specific scenario: hard-coded props, mock data, providers, recorded callbacks. Stories live next to the component in
*.story.tsx(or.ts/.jsx/.js/.vue) files; each named export is one story. - The gallery is a single page you implement to
references/gallery-spec.md: it exposeswindow.mount(params)/window.unmount()that render a story — resolved from your story files (e.g. withimport.meta.glob) — into#root. It is framework-specific and yours to own — there is no template to copy for it. - Tests are plain Playwright tests. The built-in
mount(storyId, props?)fixture (from@playwright/test) drives the gallery'swindow.mountand returns aLocatorfor the gallery root (#root). Scope the queries from there —component.getByRole('button').click(), notcomponent.click(). Nothing to scaffold for it.
Everything the component needs must be set up inside the story (it runs in the browser); everything the test asserts must be observable through the page (DOM, URL, network). Where the component takes callbacks, the story creates the state, provides the callbacks and records the state into a hidden form for the test to assert on. mount(id, props) passes plain serializable props to the story.
Setup workflow
- Detect the framework and bundler. React vs Vue decides the framework notes and example story to follow. Then:
- App runs on Vite (has
vite.config.*): the gallery is served by the existing dev server at/playwright/gallery/index.html— Vite serves any.htmlfile under the project root, the app's plugins/aliases/CSS apply automatically, andvite buildignores it. No extra server needed. - Anything else (Next.js, webpack, no dev server): run a small standalone dev server (e.g. Vite) that serves the gallery page, and point
baseURLat it. Requiresviteand the framework plugin as devDependencies.
- App runs on Vite (has
- Implement the gallery to
references/gallery-spec.md: a page at<project>/playwright/gallery/that renders the requested story into#root. Start from the worked example in the spec and the framework notes inreferences/react.md/references/vue.md. Keep story discovery (import.meta.glob) and the framework mount here — this is the only framework-specific glue, so keep it small. Import the app's global CSS the same way the app's own entry does. - Configure Playwright — add to
playwright.config.ts: