How AI is applied across API Evangelist and APIs.io. Read my AI disclosure →
API Evangelist API Evangelist
Discovery
Learnings
Guidance
Toolbox
Alignment
API Evangelist LLC

Component Test with Storybook and Vitest

calendar_today July 29, 2025 person Michael Shilman domain storybook

Component Test with Storybook and Vitest<p>You already use Storybook to build UI states. With Storybook 9, we evolved our testing workflows using the latest generation of tools. We partnered with Vitest, which ships the fastest test runner available, to build the ultimate component testing workflow. It includes:</p><p>👆 Interaction tests
♿️ Accessibility tests
🖼️ Visual tests
🛡️ Coverage reports
🚥 Test widget to run tests automatically</p><h2 id="why-component-tests">Why component tests?</h2><p>Component tests hit the sweet spot between the speed of unit tests and the in-browser fidelity of end-to-end (E2E) tests with none of the downsides.</p><p>Component tests scale to thousands of UI states with minimal maintenance. Unit tests validate logic; E2E tests are best for a few key flows. Storybook Test covers the middle: your components.</p><p>Storybook is perfect for component testing because stories already express every component variation so adding tests to those variations is easy.</p>

Component Test with Storybook and Vitest
<h2 id="the-3-types-of-component-tests">The 3 types of component tests</h2><p>In frontend development, there are three core dimensions to test. Storybook supports these tests out-of-the-box in local development and in CI.</p><ol><li>Does it work? Interaction tests</li><li>Can everyone use it? Accessibility tests</li><li>Does it look right? Visual tests</li></ol><h3 id="interaction-tests">Interaction tests</h3><p>Interaction tests simulate user behavior and assert that the component functions as expected. Storybook has supported this test type for years via the play function, but you could only run these tests when you navigated to the story. Now you can run all interaction tests across all your stories with a single click and report test statuses in the sidebar thanks to Vitest.</p>
Component Test with Storybook and Vitest
<p>Clicking on an interaction test failure in the sidebar takes you to the Interactions panel. It allows you to step forwards and backwards through the play function and inspect the story at every step along the way.</p>
Component Test with Storybook and Vitest
<div class="kg-card kg-callout-card kg-callout-card-blue"><div class="kg-callout-emoji">🚥</div><div class="kg-callout-text">All tests can run in your CI environment even without Storybook</div></div><h3 id="accessibility-tests">Accessibility tests</h3><p>Accessibility tests scan the rendered DOM to verify that it meets the Web Content Accessibility Guidelines (WCAG). These checks are implemented by axe-core, the industry standard tool.</p><p>9.0 introduces a redesigned A11y panel, improved violation highlighting, and deep linking to streamline debugging.</p>
Component Test with Storybook and Vitest
<p>The panel has been overhauled to group violations by type, and allows you to debug each failure by highlighting it in the story and providing useful info on how to fix it.</p><p>Our sister tool, Chromatic, just introduced Accessibility Regression Testing (ART) in the cloud. Most teams have preexisting accessibility debt and can’t drop everything to fix every violation. ART sets a baseline for preexisting violations and stops new WCAG bugs from being introduced. That way you can continue shipping and prioritize debt at your own pace.</p>
Component Test with Storybook and Vitest<figcaption>Detect new violations in your pull request by comparing to a WCAG baseline</figcaption>
<h3 id="visual-tests">Visual tests</h3><p>Visual tests compare the appearance of your UI before and after a code change to prevent visual bugs. The best part about visual testing is that you don’t need to write any new test code.</p><p>Storybook visual tests use Chromatic, a cloud service developed by Storybook maintainers which offers parallelization and consistency across thousands of tests.</p><p>When it detects a visual difference, It shows up as a warning in the sidebar. Clicking on that warning takes you to the Visual tests panel, which shows the image difference of the before and after states.</p>
Component Test with Storybook and Vitest
<h2 id="test-widget-test-everything-in-one-click">Test widget: Test everything in one click</h2><p>The Test widget anchors Storybook Test. One click runs all tests across all stories—or configure which types to run. Enable watch mode to instantly rerun only the affected tests when you save.</p>
Component Test with Storybook and Vitest
<p>You can then filter the sidebar to only show stories with warnings or errors. Each test type has its own debug panel. Since Storybook runs in your own browser, you can also debug using browser dev tools.</p>
Component Test with Storybook and Vitest
<h3 id="batch-testing">Batch testing</h3><p>Batch test a single story, component, or folder to speed up feedback. Perfect for large Storybooks with thousands of stories.</p>
Component Test with Storybook and Vitest
<h2 id="test-coverage">Test coverage</h2><p>Code coverage compares component code with story code to surface which lines of code & branches are executed by tests. Coverage is built into Storybook to help you ensure that your test suite covers every key UI variation.</p>
Component Test with Storybook and Vitest
<h2 id="powered-by-vitest">Powered by Vitest</h2><p>We chose Vitest because it’s the defacto successor to Jest with better performance and developer experience. Vitest’s test runner powers Storybook Test in our sidebar and enables you to run in the CLI/CI without needing Storybook at all.</p><p>Vitest will treat all your stories as tests and outputs results alongside Vitest unit tests.</p><p>What’s more, adopting Vitest unlocks a whole ecosystem of integrations for you. For instance, you can run all your Vitest tests from within Visual Studio Code and see the results inline as you edit your code. Since Storybook stories are Vitest tests, you get the same seamless workflow across component and unit tests.</p>
Component Test with Storybook and Vitest
<h2 id="try-it-today">Try it today</h2><p>Storybook Test is part of the recommended install in new Storybook 9 projects.</p><pre>npm create storybook@latest </pre><p>If you have an existing Storybook project, first upgrade it to Storybook 9. Use our automated migration wizard to help you along the way:</p><pre>npx storybook@latest upgrade </pre><p>Then add the Vitest addon to get the core component testing experience. You can also add the Accessibility addon and Visual Test addons.</p><pre>npx storybook add @storybook/addon-vitest npx storybook add @storybook/addon-a11y npx storybook add @chromatic-com/storybook </pre><p>Get full docs and examples in the Storybook Test docs.</p><h2 id="what%E2%80%99s-next">What’s next?</h2><p>Storybook 9 is the culmination of a year’s worth of work building Storybook Test. Our vision is to create the most ergonomic batteries-included UI testing tool. To that end, we’re considering the following improvements:</p><ul><li>First class MSW data mocking.</li><li>Automatic multi-viewport / theme / locale testing</li><li>Syntactic sugar for multiple test steps per story</li></ul><p>We want your feedback to help prioritize Storybook’s direction. Join the discussion on GitHub →</p>
<blockquote class="bluesky-embed"><p lang="en">Have you tried component testing in Storybook yet? We partnered with @vitest.dev to build the best tool for testing your components in the ways that matter.

👆 Interaction tests ♿️ Accessibility tests 🖼️ Visual tests 🛡️ Coverage reports 🚥 Test widget to run tests automatically</p>— Storybook (@storybook.js.org) 2025-07-29T19:17:01.646Z</blockquote></figure>

open_in_new Read original post