During large-scale frontend modernization efforts, teams frequently convert dozens of components from JavaScript to TypeScript. While stateful components require deep assertions on reactive state, mutations, and API calls, pure static presentation components—such as legends, badges, and informational tooltips—serve a different purpose: preserving visual markup and design token consistency.
In my recent contribution to GitLab's Contributors Platform (MR !3195), I converted the LegacyCategoryLegend component to TypeScript and established an automated snapshot test suite using Vitest.
Here is an architectural look at how lightweight snapshot testing protects visual UI elements during progressive migrations.
1. The Component: Historical Contributor Tier Badges
On the Top Annual Contributors leaderboard, the platform honors historical contribution milestones (prior to the current point-based scoring system).
LegacyCategoryLegend.vue displays color-coded badges mapping to historical recognition tiers:
- SuperStar (Warning badge): More than 75 merged MRs
- Star (Info badge): Between 11 and 75 merged MRs
- Enthusiast (Secondary badge): Between 5 and 10 merged MRs
<!-- LegacyCategoryLegend.vue --> <script setup lang="ts"> import { BBadge } from 'bootstrap-vue-next'; </script> <template> <div class="text-muted small mt-2"> <span class="me-3"> <BBadge variant="warning">SuperStar</BBadge> More than 75 merged MRs </span> <span class="me-3"> <BBadge variant="info">Star</BBadge> Between 11 and 75 merged MRs </span> <span> <BBadge variant="secondary">Enthusiast</BBadge> Between 5 and 10 merged MRs </span> </div> </template> Adding <script setup lang="ts"> ensures typed integration with bootstrap-vue-next component exports without any runtime overhead.
2. Fast, Deterministic Snapshot Testing with Vitest
For static presentation components without props or reactive state, writing redundant DOM query assertions for every badge text can be unnecessarily verbose.
Instead, a deterministic snapshot test using shallowMount provides 100% coverage while locking down the exact HTML structure and CSS utility classes:
// LegacyCategoryLegend.spec.ts import { shallowMount } from '@vue/test-utils'; import LegacyCategoryLegend from './LegacyCategoryLegend.vue'; const createWrapper = () => shallowMount(LegacyCategoryLegend, { global: { renderStubDefaultSlot: true }, }); it('matches the snapshot', () => { expect(createWrapper().html()).toMatchSnapshot(); }); Why this pattern works well:
- Instant Regression Detection: Any unintentional change to Bootstrap badge variants (
warning,info,secondary), spacing classes (me-3,mt-2), or milestone descriptions is immediately flagged by the Vitest snapshot runner. - Stub Slot Rendering: Using
global: { renderStubDefaultSlot: true }ensures that child slot content (like"SuperStar") is preserved inside<b-badge-stub>rather than discarded. - Zero Boilerplate: The spec is concise, clean, and runs in milliseconds.
3. Snapshot Output Verification
When executed, Vitest records the rendered template in __snapshots__/LegacyCategoryLegend.spec.ts.snap:
// Vitest Snapshot v1 exports[`matches the snapshot 1`] = ` "<div class="text-muted small mt-2"> <span class="me-3"> <b-badge-stub variant="warning">SuperStar</b-badge-stub> More than 75 merged MRs </span> <span class="me-3"> <b-badge-stub variant="info">Star</b-badge-stub> Between 11 and 75 merged MRs </span> <span> <b-badge-stub variant="secondary">Enthusiast</b-badge-stub> Between 5 and 10 merged MRs </span> </div>" `; This guarantees visual and structural stability as the underlying UI library upgrades across minor and major versions.
Key Takeaways
- Use Snapshot Tests for Pure Presentational UI: When a component has no internal logic or conditional branching, snapshot testing offers high confidence with minimal test maintenance.
- Always Enable
renderStubDefaultSlot: In Vue Test Utils, shallow mounting child components can hide slot text unlessrenderStubDefaultSlot: trueis configured. - Every Component Counts: Converting even small UI components to TypeScript ensures the whole codebase maintains 100% type coverage without gaps.
Check out the merged Merge Request on GitLab:
👉 GitLab Contributors MR !3195
Author: Manish Kumar
GitLab: @manishpatel00


