@vizzly-testing/vitest
v0.1.1
Published
Drop-in replacement for Vitest visual testing - powered by Vizzly
Downloads
492
Maintainers
Readme
@vizzly-testing/vitest
Drop-in replacement for Vitest visual testing - powered by Vizzly
Overview
This package completely replaces Vitest's native visual testing with
Vizzly's powerful platform. Just add the plugin and continue using Vitest's
standard toMatchScreenshot API - no code changes required!
True drop-in replacement. Zero API changes. Maximum power.
Features
- ✅ Native Vitest API - Use
toMatchScreenshot- no custom matchers! - 🎨 Per-Screenshot Properties - Add metadata for multi-variant testing
- 🏃 TDD Mode - Interactive local dashboard with live comparisons
- ☁️ Cloud Mode - Team collaboration with visual reviews
- 🚀 CI/CD Ready - Parallel execution and baseline management
- 🔬 Better Diffing - Powered by honeydiff (Rust-based, faster than pixelmatch)
Installation
npm install -D @vizzly-testing/vitest @vizzly-testing/cliQuick Start
1. Configure Vitest
Add the Vizzly plugin to vitest.config.js:
import { defineConfig } from 'vitest/config'
import { vizzlyPlugin } from '@vizzly-testing/vitest'
import { playwright } from '@vitest/browser-playwright'
export default defineConfig({
plugins: [vizzlyPlugin()],
test: {
browser: {
enabled: true,
instances: [
{
browser: 'chromium',
provider: playwright()
}
]
}
}
})2. Write Tests
Use Vitest's native toMatchScreenshot matcher - no changes needed:
import { expect, test } from 'vitest'
import { page } from 'vitest/browser'
test('homepage looks correct', async () => {
// Render your component/page
document.body.innerHTML = '<div class="hero">Hello World</div>'
// Basic screenshot
await expect(page.getByRole('heading')).toMatchScreenshot('homepage.png')
// With properties for multi-variant testing
await expect(page.getByRole('heading')).toMatchScreenshot('hero-section.png', {
properties: {
theme: 'dark',
viewport: '1920x1080'
},
threshold: 5
})
})3. Run Tests
TDD Mode (local development with live dashboard):
# Terminal 1: Start Vizzly TDD server
npx vizzly dev start
# Terminal 2: Run tests
npx vitest
# Open dashboard at http://localhost:47392Note: New screenshots automatically create baselines on first run and pass! You can review them
in the dashboard at http://localhost:47392/dashboard. Future runs will compare against the
baseline.
Cloud Mode (CI/CD with team collaboration):
npx vizzly run "npx vitest" --waitAPI Reference
Plugin Options
The plugin requires no configuration, but you can pass options if needed:
import { vizzlyPlugin } from '@vizzly-testing/vitest'
// Simple - just add the plugin
vizzlyPlugin()
// Or with options (rarely needed)
vizzlyPlugin({
// Plugin-specific options can go here
})Screenshot Options
All options are passed directly to toMatchScreenshot:
await expect(page).toMatchScreenshot('screenshot.png', {
// Custom properties for multi-variant testing
properties: {
theme: 'dark',
language: 'en',
userRole: 'admin'
},
// Comparison threshold (0-100)
threshold: 5,
// Full page capture
fullPage: true
})Available Options:
properties(object) - Custom metadata for signature-based baseline matchingthreshold(number, 0-100) - Acceptable difference percentage (default: 0)fullPage(boolean) - Capture full scrollable page instead of viewport
Multi-Variant Testing
Use properties to test the same component in different states:
test('button variants', async () => {
// Light theme
document.body.classList.add('theme-light')
await expect(page.getByRole('button')).toMatchScreenshot('button.png', {
properties: { theme: 'light' }
})
// Dark theme
document.body.classList.remove('theme-light')
document.body.classList.add('theme-dark')
await expect(page.getByRole('button')).toMatchScreenshot('button.png', {
properties: { theme: 'dark' }
})
})Vizzly will manage separate baselines for each variant using signature-based matching: button.png|theme:dark|...
How It Works
This plugin completely replaces Vitest's native screenshot testing by:
- Extending
expectAPI - Registers a customtoMatchScreenshotmatcher that overrides Vitest's - Disabling native system - Sets
screenshotFailures: falseto prevent conflicts - Direct HTTP communication - Screenshots POST directly to Vizzly server from browser context
TDD Mode
- Plugin injects setup file that extends
expectwith custom matcher - Your test calls
toMatchScreenshot→ captures screenshot in browser - Matcher POSTs screenshot to local Vizzly TDD server
- Server compares using honeydiff → returns pass/fail result
- Dashboard shows live results at
http://localhost:47392/dashboard - Accept/reject changes in UI → baselines updated in
.vizzly/baselines/
Cloud Mode
- Same custom matcher captures screenshot in browser
- POSTs to Vizzly server which queues for upload
- After tests complete → uploads to Vizzly cloud
- Team reviews changes in web dashboard
- Tests always pass - comparison happens asynchronously in cloud (use
--waitflag to fail on visual changes)
TypeScript
Full TypeScript support included! The plugin automatically extends Vitest's types:
import { expect, test } from 'vitest'
import { page } from 'vitest/browser'
test('typed screenshot', async () => {
await expect(page).toMatchScreenshot('hero.png', {
properties: {
// Full autocomplete support
theme: 'dark',
viewport: '1920x1080'
},
threshold: 5,
fullPage: true
})
})Vizzly Direct Integration
You can also use Vizzly without the comparator by calling vizzlyScreenshot() directly:
import { vizzlyScreenshot } from '@vizzly-testing/cli/client'
test('manual screenshot', async () => {
let screenshot = await page.screenshot()
await vizzlyScreenshot('homepage', screenshot, {
properties: { theme: 'dark' }
})
})Comparator approach (this package):
- ✅ Native Vitest API (
toMatchScreenshot) - ✅ Integrated with Vitest's snapshot management
Direct approach:
- ✅ Full control over screenshot capture
- ✅ Works with any test runner
- ✅ More explicit
Examples & Tests
See the tests in this package at tests/:
- vitest-plugin.spec.js - Unit tests for plugin configuration and comparator function
- e2e/ - End-to-end test project running actual Vitest tests with the plugin
The E2E tests serve as both validation and a working example. Run them with:
# From clients/vitest directory
npm install
npm run test:unit # Run unit tests
npm run test:e2e # Run E2E tests (requires vizzly dev start)
npm test # Run all testsTroubleshooting
"Vizzly not available" message
Make sure you're running tests with either:
vizzly dev start(TDD mode)vizzly run "npx vitest"(cloud mode)
Screenshots not appearing in dashboard
- Check
vizzly statusfor TDD, make sureVIZZLY_TOKENis set for cloud capture - Verify API token is set:
npx vizzly whoami - Check console for error messages
License
MIT © Stubborn Mule Software
