Unit Testing: Automated Checks That Prove Each Component Still Works
Learn how small automated checks confirm that one design system component, such as a Button, looks and behaves as its spec says in every state.
In short
Unit testing for a design system uses small automated checks to confirm that one component, such as a Button, looks and behaves as its spec says in every state.
10 min readFree
What You Will Learn
A design system is a shared set of parts, such as Buttons, text fields, and badges, that many product teams reuse. When one shared part changes, every screen that uses it can change too. Unit testing is a way to catch a broken part before it reaches those teams.
In this lesson you will learn what a unit test checks for one component, how to write a basic one in clear steps, and what makes a set of tests useful for a design system. Here, a unit is one small piece tested by itself, not a whole page.
Key words
- Unit test
- An automated check that tests one small part, such as one Button, all by itself.
- Component
- A reusable building block, such as a Button or a Dropdown, that teams place into their screens.
- Props
- Settings you pass into a component to change how it looks or acts, such as turning on its disabled setting.
- State
- The current condition of a component, such as default, disabled, loading, or error.
- Variant
- A named version of a component, such as a primary Button or a secondary Button.
- Render
- To show the component on screen so its output can be checked.
- Assert
- To check that a result matches what the spec expects. A test passes or fails based on its checks.
- Regression
- A bug that appears in something that used to work, often after a change made somewhere else.
- Snapshot
- A saved copy of a component's output that later test runs compare against.
- Test suite
- The full set of tests a team keeps for its design system.
What does a unit test check?
Many teams start their automated testing here, because a unit test looks at one small piece, such as a single component, and is the simplest kind of automated check to set up.
For a UI component, a unit test usually asks three questions. Does the component show the right output for the settings it was given? Does it react correctly when a user clicks or presses a key? Does it handle unusual cases? A test can confirm that the right text appears, that clicking works, and that every supported setting gives a predictable result.
Expected output
For a given setting or state, the component shows what the spec says, such as the right label, icon, or color.
Correct reactions
When a user clicks or presses a key, the component does its job. It also does nothing it should not do.
Unusual cases
Teams also ask what happens when a user clicks things out of order, or when a request to a server fails.
How do you test each state?
A design system component often comes in several states.
Default
The normal look and behavior when no special setting is turned on.
Disabled
The control is switched off. Clicks should do nothing, and users should be able to see that it is off.
Loading
The control is busy, for example while an action is still running. The spec should say whether clicks are blocked.
Error
Something went wrong. The spec should say what the component shows and whether it still accepts input.
Each state needs its own check. A passing test for the default state says nothing about the disabled state. A disabled Button often needs both a visual check and a separate unit test for that same state.
How do you write a basic component test?
Most unit tests follow the same three-part pattern. First you set up the situation, then you act, then you check the result. Testers often call these arrange, act, and assert.
- 1
Set up
Place the component into the test with the settings you want to check, such as disabled turned on. Write down what the spec says should happen before you start.
- 2
Simulate the user
Act the way a person would. Click the Button, press a key, or type into a field. Testing tools can send these actions for you.
- 3
Assert the result
Check what changed. Is the Button marked as disabled? Did the click action run the number of times the spec allows? Do not check things the spec does not mention.
- 4
Repeat for each state
Change one setting, such as loading, and run the same three parts again. Keep each test focused on one state.
Keep each test independent. A test should not depend on what another test did first, so that any single test can fail or pass on its own.
Should you test behavior or implementation?
Good tests check behavior, which means what users see and do. Weak tests check implementation details, which means the internal code that builds the component. The difference matters, because internal code gets reorganized often.
Behavior (test this)
The label is visible, the Button is marked as disabled, and a click does nothing while it is disabled.
Implementation (avoid this)
Internal variable values, the names of internal helpers, or a particular code class that a builder picked.
Why it matters
A test that checks internals can break when someone tidies the code, even if no user sees a change. A test of behavior keeps working through that kind of cleanup.
Tip
If a test fails only because someone renamed something no user can see, the test was checking implementation. Rewrite it to check what the user sees and does.
What makes a design system test suite useful?
One test for one Button is a start. A useful test suite covers the whole component and runs without anyone having to remember it.
Every variant and state
Cover each named version and each state in the spec, not only the most common one. Tests should also check borderline cases, such as the largest allowed text.
Runs on every change
Tests should run automatically whenever someone changes a component. Many teams connect this to their build process, often called CI, short for continuous integration.
Catches regressions early
A regression is a bug in something that used to work. Catching it before a component is released helps keep the broken version from reaching product teams.
Records intent
Good tests also act as notes. They tell the next designer or builder what the component is supposed to do.
Tip
Name each test after the behavior it protects, such as 'disabled Button ignores clicks'. When a test fails, the name tells the team what broke.
Snapshot and Visual Tests, and Their Limits
Many teams also use snapshot tests and visual tests. A snapshot test saves a copy of the component's output and flags any later difference. It checks that the output stayed the same, not that the output is correct. A visual test compares screenshots of the component as it appears in a browser.
Snapshots are quick
They are quick to write, which also makes them easy to overuse. Keep them for output that is stable and short enough to review.
Snapshots are noisy
They check every detail, including details no user would notice.
Snapshots can miss looks
A saved copy of the code output can still match after a color or layout change, because the saved text does not show the visual style.
Snapshots skip behavior
They do not test clicks, interactions, or accessibility on their own.
Visual tests see the screen
They compare screenshots, which catches style changes, but they are usually slower to run and to maintain.
A sensible rule is to build the base from behavior tests, then add visual checks where looks matter. Some checks still need a person. Unit tests usually run without a real browser, so element sizes, positions, and responsive rules cannot be fully tested that way.
Tip
A passing snapshot does not prove that a Button looks right. Open the component and look at it too.
Check Your Understanding
Quick check
A teammate changes the padding and refactors the internal code of your design system Button. The next day, the Button no longer runs its click action when loading is false. Which kind of unit test would have caught this before release, and what would it assert?
Quick check
A designer changes a text field's border from grey to red. The team's snapshot test for that field still passes. What is the most likely reason, and what would catch the change?
Wrap Up
A unit test checks one component in one state at a time. It acts like a user, checks the result against the spec, and runs again every time someone changes the part. Start with behavior tests for each variant and state, and add snapshot or visual checks where looks matter. Accessibility checks, such as screen reader support and contrast, are covered in a separate lesson.
Sources
- Reshaped: Testing design systems in 2025
- Lost Pixel: What is Design System Testing? (2024 guide)
- ResumeLens: React Testing Library Patterns
- NamasteDev: What Is React Testing Library and Why It Changed React Testing
- JumpCloud Engineering: How to Write Good UI Component Tests
- Overctrl: A Beginner's Guide to Unit Testing in Frontend Development
- Plain English: A Primer on Unit Testing in UI
- Maya Shavin: Effective Visual Regression Testing for Developers: Vitest vs Playwright
- Percy: Snapshot Testing: Should You Go For It in 2026?
- SpecStory: What is snapshot testing?
- UI Verify: Snapshot testing vs visual regression testing
- Meticulous: How to Set up Visual Regression Tests
- DZone: How Do You Test A Design System? Advanced Topics
- dev.co: React Testing Library: Behavior-Driven Component Testing
Last updated on 17 November 2026
Related lessons
Learn with me
Every lesson is free. I add new ones as I learn. Have a question, or want help with your own work? Book a short call.