TechBuzzPro

Chromatic Test Responsive Layouts at Custom Screen Sizes?

· 5 min read · Windows & PC

In this article
  1. Why Chromatic Matters for Responsive Design
  2. How to Set Custom Viewport Sizes
  3. Choosing the Right Viewport Sizes
  4. Common Problems and How to Avoid Them
  5. Comparing Chromatic to Other Tools
  6. Costs and Limits
  7. Safety and Accuracy Considerations
  8. Alternatives to Chromatic
  9. Frequently Asked Questions

Yes, Chromatic can test responsive layouts at custom screen sizes. You define the viewport width and height per story or globally in your Storybook configuration, and Chromatic captures snapshots at those exact dimensions. This lets you catch visual regressions at breakpoints your design system actually uses, not just the default 1280×720.

Why Chromatic Matters for Responsive Design

Chromatic is a visual regression testing service built for Storybook. It takes screenshots of your components and compares them pixel-by-pixel against a baseline. When you change code, Chromatic flags any visual difference so you can approve or reject it. For responsive layouts, the challenge is that a component can look perfect on desktop but break at tablet or mobile widths. Testing at custom sizes means you simulate those exact conditions in a real browser, so overflow, hidden elements, and spacing issues become visible before they reach production.

How to Set Custom Viewport Sizes

There are two main ways to control viewport size in Chromatic: per-story parameters and global configuration. Both are done through Storybook's parameters API.

  • Per-story: Add parameters: { viewport: { defaultViewport: 'mobile' } } to a story, or use parameters: { chromatic: { viewports: [375, 768] } } to test multiple widths for that story.
  • Global: In .storybook/preview.js, set parameters: { chromatic: { viewports: [320, 768, 1440] } } to apply those sizes to every story.
  • Custom viewport presets: In .storybook/preview.js, you can define named viewports like iPhoneSE: { name: 'iPhone SE', styles: { width: '375px', height: '667px' } } and then reference them in stories.

When you specify multiple viewports, Chromatic captures a separate snapshot for each one, and each is compared independently against its own baseline. This means a change that only affects mobile will not be masked by desktop results.

Choosing the Right Viewport Sizes

Pick sizes that match your actual breakpoints, not generic device names. If your CSS uses media queries at 640px, 768px, and 1024px, test at those exact values. Also include the smallest and largest sizes your design supports. For most sites, 320px (small phones), 768px (tablets), and 1440px (desktop) cover the critical ranges. Avoid testing at every possible width—each extra viewport increases snapshot count and review time.

Common Problems and How to Avoid Them

One frequent issue is that Chromatic uses a headless browser, which may not perfectly match a real device's rendering. For example, scrollbars can differ, affecting layout width. To mitigate, set parameters: { chromatic: { disableScroll: true } } if you need to capture full-page screenshots without scrollbars. Another problem is that custom viewports may not trigger device-specific CSS like @media (hover: none)—those are not simulated. Also, be aware that Chromatic's default delay is 0ms; if your component animates or loads async content, use parameters: { chromatic: { delay: 300 } } to wait before capturing.

Comparing Chromatic to Other Tools

Chromatic is often compared to Percy and Applitools. Percy also supports custom viewports via its SDK, but requires more manual configuration. Applitools offers visual AI that can ignore dynamic regions, but it is heavier and more expensive. Chromatic's advantage is its tight integration with Storybook—you do not need to write separate test scripts; you just add parameters to your stories. It also provides a free tier for open-source projects, which makes it accessible for small teams.

Costs and Limits

Chromatic has a free plan that includes 5,000 snapshots per month, which is enough for small projects. Paid plans start at around $149 per month for 10,000 snapshots, with higher tiers for larger teams. Each viewport in a story counts as one snapshot, so testing 3 viewports per story triples your usage. If you have a large component library, this can add up quickly. You can reduce costs by testing only the stories that are likely to be affected by responsive changes, rather than every story at every breakpoint.

Safety and Accuracy Considerations

Visual regression testing is not a substitute for manual testing on real devices. It only catches visual differences—it cannot tell you if a layout is semantically correct or if touch targets are large enough. Also, Chromatic's screenshots are taken at a fixed device pixel ratio (usually 1x), so high-DPI rendering differences may not be captured. For critical responsive behavior, pair Chromatic with real browser testing using tools like BrowserStack or Playwright.

Alternatives to Chromatic

If you do not use Storybook, you can use Playwright or Cypress to take screenshots at custom viewports. Playwright's page.setViewportSize() and page.screenshot() give you full control. Cypress has a cy.viewport() command. These tools require more setup but are more flexible. For a purely CSS-based approach, you can use Chrome's DevTools to manually resize and inspect, but that does not provide automated regression detection.

Frequently Asked Questions

Can I test at a custom width like 375px?

Yes, you can specify any width in pixels using the viewports parameter. For example, viewports: [375] will capture a snapshot at exactly 375px wide. You can also set a custom height by defining a named viewport with styles.

Does Chromatic support device emulation like mobile?

Chromatic does not emulate mobile devices; it only changes the viewport size. Features like touch events or device pixel ratio are not simulated. For true device emulation, you would need a separate tool.

Why does my layout look different in Chromatic than in my browser?

This usually happens because of scrollbars, font rendering, or missing web fonts. Chromatic uses headless Chrome, which may not load local fonts. You can use parameters: { chromatic: { pauseAnimation: true } } to freeze animations, and ensure fonts are loaded via @import or a web font loader.

Can I test responsive layouts without Storybook?

Yes, but you would need to use a different tool like Playwright or Cypress. Chromatic is specifically designed for Storybook, so if you are not using Storybook, those alternatives are more appropriate.

How many snapshots will I use if I test at 3 viewports?

Each story at each viewport counts as one snapshot. So if you have 100 stories and test at 3 viewports, that is 300 snapshots per run. Be mindful of your plan's snapshot limit.