feat(vitest): add formatTitle and Storybook 10 viewport globals#330
feat(vitest): add formatTitle and Storybook 10 viewport globals#330kasperpeulen wants to merge 19 commits intomainfrom
Conversation
Allow Vitest archives to map files to components, tests to stories, and named snapshots to Chromatic modes so multi-snapshot tests render as one logical test group.
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #330 +/- ##
==========================================
+ Coverage 91.48% 91.66% +0.17%
==========================================
Files 22 22
Lines 611 624 +13
Branches 114 120 +6
==========================================
+ Hits 559 572 +13
Misses 49 49
Partials 3 3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
….viewport - Emit globals.viewport (width-height) alongside existing parameters.viewport - Map globals string to w*h* archive snapshot names in preview fetch - Guard defaultViewport fallback when parameters.viewport is absent - Clarify formatTitle option JSDoc
…eview mapping globals.viewport now matches defaultViewport and snapshot filenames. Preview uses string globals as the archive key directly.
42b7490 to
5a9f5f5
Compare
SB 10 reads options (not legacy viewports), so archives fell back to MINIMAL_VIEWPORTS (e.g. 320x568) in the toolbar. Add viewport type for the addon; keep defaultViewport for archive preview fetch fallback.
| } | ||
| } | ||
|
|
||
| function getTitlePath(test: TestCase, formatTitle?: ResolvedOptions['formatTitle']): string[] { |
There was a problem hiding this comment.
I would actually propose function formatTitle(test: TestCase): string as public API. Internally its default value would be the current getNames. This would allow more complex configuration for user.
There was a problem hiding this comment.
@kasperpeulen how does the Storybook Chromatic integration set the titles? Does it use the CSF's component property? Should we have have something similar here?
import { test } from "vitest";
import { setComponent } from "@chromatic-com/vitest";
import Accordion from "../src/components/accordion";
setComponent("Accordion");
test("opens when...", ...)
test("closes when ...", ...)This setComponent would internally just set task.meta.__chromatic_componentName, that we would then use in getNames and possible user defined formatTitles(test: testCase, componentName?: string): string.
There was a problem hiding this comment.
Let's talk about this on wednesday!
There was a problem hiding this comment.
As these Viewport fixes affect all test runners, could you move them to a separate PR? We'll need changesets to reference that PR in Playwright and Cypress release notes too.
AriPerkkio
left a comment
There was a problem hiding this comment.
I think these viewport changes got Chromatic baselines changed, and made main fail. Some globals + viewports parts in URLs made stories of published Storybook not load.
We should revert these tests once all fixes are complete:
packages/cypress/tests/cypress/e2e/storybook.cy.tspackages/playwright/tests/storybook.spec.ts
See #331
Summary
formatTitlecallback for projects that want to customize the title generated from Vitest tests.[projectName?, filePath, ...testPath].globals.viewportasw{width}h{height}, matching the archive snapshot key) so archive stories open in the captured viewport.Why
Vitest gives the plugin a title path array. For example:
["app/auth/sign-in/page.test.tsx", "shows the magic link error banner"]Today
writeTestResultturns that array into the Storybook title by stripping test file extensions and joining the parts with/:app/auth/sign-in/page/shows the magic link error bannerThat default is still correct and remains unchanged. The problem is how this appears in Chromatic: Chromatic commonly uses the final
/segment as the visible leaf/display name in the UI. For the default title above, that means the row users scan is just:shows the magic link error bannerThe useful route/file context (
app/auth/sign-in/page) is hidden in the parent path, which makes Vitest snapshots harder to scan in Chromatic.formatTitlelets a project opt into a custom title string beforewriteTestResultwrites the archive. Returning a single string also keeps the API simpler than exposing the internal title-path array. If a project wants Storybook grouping, it can still include/in the returned string.API
This can turn the default path:
app/auth/sign-in/page/shows the magic link error bannerinto one visible title:
app∕auth∕sign-in → shows the magic link error bannerNamed snapshots remain separate stories under that title, for example:
app∕auth∕sign-in → shows the magic link error banner / Dark desktopViewports
Storybook 10 selects the active viewport through
globals.viewport, together withparameters.viewport(available sizes / default). See Defining the viewport for a story.For archive stories we emit the captured archive viewport key as the story global, e.g.:
{ "globals": { "viewport": "w1280h720" } }That value matches the archive snapshot filename segment, so the renderer can fetch the matching rrweb snapshot. The archive still includes
parameters.viewport.viewportsandparameters.viewport.defaultViewportso the viewport option is defined for Storybook.Test plan
yarn vitest run packages/shared/src/write-archive/stories-files.test.ts packages/shared/src/write-archive/index.test.ts packages/vitest/src/node/commands.test.tsyarn lint@chromatic-com/vitestinto a consumer app and confirmed Vitest browser tests +chromatic --vitestcomplete successfully (expect baseline diffs until snapshots are accepted).