component-tests
Component tests
Each package suite starts one story server, a plain Vite dev server for packages/stories, and every worker shares it (packages/stories/playwright/config.ts:17-26; CI uses 2 workers, config.ts:15). With fullyParallel (config.ts:12), the tests of one file run on several workers in any order. There is no backend: server, SDK and sync modules are in-page mocks. Every test gets a fresh context, and mount is a full page navigation to the frame (packages/stories/playwright/story.ts:37). Slow or flaky component tests in CI have come from five sources: the shared server's dependency optimizer, module resolution that depended on the install layout, a cold first story, a product race (the mobile drawer), or an action that ran before the UI reached the state it needed.
Read these first rather than repeating them:
packages/stories/playwright/README.mdcovers layout, run commands and "Adding a test".packages/app/AGENTS.md"Tests" decides whether a test should exist, when theme or direction cases are allowed, and that a regression test must fail before the fix.packages/app/e2e/AGENTS.md"Test Hygiene" applies here unchanged.
Stories and mount
- Location. Put the story next to the production component under
src/ofui,session-ui,apporgui-plugins. The story server indexes*.stories.tsxfiles in those four trees (packages/stories/vite.config.ts,src/index-plugin.ts), reading each file'sexport default { title, id }and itsexport conststories from source. - Story ID. It is the meta
idif one is set (packages/session-ui/src/components/session-review.stories.tsx:25). Otherwise it is the kebab-casedtitle, then--, then the kebab-cased export. Check it athttp://127.0.0.1:6006/?id=<id>on a running story server (bun --cwd packages/stories dev). A wrong ID renders nothing (the page throwsNo story has the id …), somountwaits out its full 30 s root wait (story.ts:39) and then fails. - URL args.
mount(id, { args, globals })writes them into the URL askey:valuepairs (story.ts:20-36). The frame reads every one: a value becomes a boolean or number when the story's own default for that key is one,!true/!false/!null/!undefinedkeep Storybook's meaning, and anything else stays text. - Give every arg a typed default. With
args: { split: false }(session-review.stories.tsx:129),split:falsearrives as a boolean. - A value containing
;would split the pair list, so pick such values inside the story, from ascenario-style arg. - Globals are
theme(defaultlight),directionandlocale(packages/stories/src/frame.tsx). - Portaled UI.
mountreturns#story-root. Dialogs, menus and drawers render outside it, so locate them frompage(packages/app/component-tests/composer.spec.ts:101-107). - Server state comes from the
resolve.aliaslist inpackages/stories/vite.config.ts, which points app server, SDK and sync modules atpackages/stories/mocks/app/**. Extend a mock rather than adding a server. The app's components lab renders the same stories inside the real app instead, so a story must not depend on a mock-only export. Tests reset mock state on every mount. - Requests the mocks do not answer. Route them with
page.route. That includes server endpoints such as/api/fs/read(packages/session-ui/component-tests/read-image.spec.ts:20) and external URLs such as favicons (packages/session-ui/component-tests/markdown.spec.ts:217).
Fixtures imported by URL
Use these when the contract is not visible in a story, such as construction counts, unmount races or cache state.
- Fixture file. Put a
*.fixture.tsxfile incomponent-tests/or beside the source it drives (packages/gui-plugins/src/browser/panel.fixture.tsx). It exports amountX()that renders into a host with adata-testid(packages/session-ui/component-tests/basic-tool.fixture.tsx:7-46). Never end the name in.specor.test. - Mount it in three steps.
- Mount a light story first, so that the app styles load.
- Import the fixture and call it inside
page.evaluate. - Locate it with
page.getByTestId(packages/session-ui/component-tests/basic-tool.spec.ts:30-36).
It renders outside the frame's decorators, so it must supply its own providers (packages/app/component-tests/browser-pane-restore.spec.ts:69-83).
- Module URLs. Build each one with
sourceURL(new URL("<relative path with extension>", import.meta.url))(story.ts:10), written as a string literal in the spec (browser-pane-restore.spec.ts:6-24). Any other spelling either fails to load or loads a second module instance whose contexts the page never provides; a hand-written/@fs//...is the documented second-instance case (story.ts:6-9). The literal also matters for pre-bundling (next section). - Aliases. A module imported by URL is the real file. App code that reaches the same module through a
vite.config.tsalias gets the mock (browser-pane-restore.spec.ts:95-96). Decide which of the two the contract needs. - **Spec imp
