Testing shared Vue components: unit, a11y, visuals?
ability to architect comprehensive component testing.
unit tests (Vitest/Jest) for logic, accessibility tests (axe, Pa11y) for ARIA/keyboard, visual regression (Percy, Chromatic) for rendering.
Why it exists
Shared component libraries are high-leverage; a bug in one component breaks 10+ consumer apps. Testing must cover behavior (props, events, state), accessibility (blind and keyboard-only users), and appearance (no unintended styling drift). Manual testing alone is slow and unreliable.
The mental model
Testing layers. Layer 1 (unit): does the component logic work? Does it handle props, emit events, manage internal state? Layer 2 (accessibility): does the DOM include required ARIA attributes? Can keyboard users navigate? Do screen readers announce content correctly? Layer 3 (visual): do CSS changes unexpectedly break layout, colors, or typography? Each layer catches different issues.
How it works
Use Vitest for unit tests: mount the component, pass props, trigger events, assert output. Use axe-core (or Pa11y) to audit the rendered DOM for accessibility violations (missing labels, invalid ARIA, keyboard traps). Use Percy or Chromatic to capture screenshots on each PR, compare against baseline, and flag pixel-level changes. Integrate all three into CI: unit tests must pass, a11y audit must have zero violations (or a documented allowlist), visual regression must be approved before merge. Storybook is a common hub: write one .stories.vue file per component, run unit tests against it, run a11y audit on the story, and Percy compares each story across browsers and screen sizes.
When it matters
Public component libraries (Material UI, Chakra) do all three rigorously. Internal libraries benefit too; a color change in a Button component should be caught before it ships to 5 apps. Small teams may start with unit + a11y, add visual regression later.
A concrete example
A Button component library. Unit test: Button accepts label, variant (primary, secondary), disabled state; @click emits click event; disabled state disables interaction. A11y test: Button has role="button", aria-label if no text, keyboard focusable (tabindex handled). Visual test: Storybook stories cover all variants (primary enabled, primary disabled, secondary enabled, secondary disabled), Percy captures screenshots on Chrome/Firefox/Safari, and on CSS changes, Percy flags the diff. If a dev changes --button-padding from 8px to 16px, Percy alerts; reviewer approves or rejects the change.
Interview question
Your component library's CI should fail if: (A) a unit test fails, (B) an a11y audit finds violations, (C) Percy detects pixel changes, (D) coverage drops below 80%. Which must you enforce, and which should you allow with review?
- a.Enforce A and B, allow C and D with reviewCorrect
- b.Enforce A and C; allow B and D with review
- c.Enforce A, B, and C; D is optional
- d.Enforce only A; a11y/visual are slower to implement
Why? this is the answer
Unit and a11y tests (A, B) are gates: no a11y violations should ship, and broken tests block merge. Visual changes (D) need review (they may be intentional improvements) but shouldn't auto-fail. Coverage thresholds (B) are nice-to-have, not hard gates. Option A balances rigor with pragmatism.
Just read this? Test yourself on what you have been reading.
Read the original → storybook.js.org
- #component-library
- #testing-strategy
- #accessibility-testing
- #visual-regression
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles