@blazediff/matcher
Core matcher logic for visual regression testing. Provides snapshot comparison with multiple algorithms, framework-agnostic APIs, and snapshot state tracking.
Features
- Multiple comparison methods:
core,core-native,ssim,msssim,hitchhikers-ssim,gmsd - Flexible input types: File paths or image buffers
- Snapshot state tracking: Reports added/matched/updated/failed status
- Configurable thresholds: Pixel count or percentage-based
- Framework-agnostic: Core logic for Jest, Vitest, Bun integrations
- Rich comparison results: Diff counts, percentages, and similarity scores
Installation
npm install @blazediff/matcher
Quick Start
import { getOrCreateSnapshot } from '@blazediff/matcher';
const result = await getOrCreateSnapshot(
imageBuffer, // or file path
{
method: 'core',
failureThreshold: 0.01,
failureThresholdType: 'percent',
},
{
testPath: '/path/to/test.spec.ts',
testName: 'should render correctly',
}
);
console.log(result.pass); // true/false
console.log(result.snapshotStatus); // 'added' | 'matched' | 'updated' | 'failed'
console.log(result.diffPercentage); // e.g., 0.05
API Reference
getOrCreateSnapshot(received, options, testContext)
Main function for snapshot comparison.
| Parameter | Type | Description |
|---|---|---|
received |
ImageInput | Image to compare (file path or buffer with dimensions) |
options |
MatcherOptions | Comparison options (see below) |
testContext |
TestContext | Test information (testPath, testName) |
Returns: Promise<ComparisonResult>
MatcherOptions
| Option | Type | Default | Description |
|---|---|---|---|
method |
ComparisonMethod | - | Comparison algorithm to use |
failureThreshold |
number | 0 | Number of pixels or percentage difference allowed |
failureThresholdType |
'pixel' | 'percent' | 'pixel' | How to interpret failureThreshold |
snapshotsDir |
string | '__snapshots__' | Directory to store snapshots (relative to test file) |
snapshotIdentifier |
string | - | Custom identifier for the snapshot file |
updateSnapshots |
boolean | false | Force update snapshots (like running with -u flag) |
diff |
Omit<DiffOptions, "output"> | - | threshold, antialiasing (default true), metric, mask, window and render. See @blazediff/core. Used by core, core-wasm and core-native. |
ssim |
Omit<SsimOptions, "output"> | - | windowSize, k1, k2, bitDepth, msSsim and hitchhikers. See @blazediff/ssim. Used by ssim, msssim and hitchhikers-ssim. |
gmsd |
Omit<GmsdOptions, "output"> | - | downsample and c. See @blazediff/gmsd. Used by gmsd. |
runInWorker |
boolean | true | Run image I/O and comparison in a worker thread for better performance |
ComparisonResult
| Field | Type | Description |
|---|---|---|
pass |
boolean | Whether the comparison passed |
message |
string | Human-readable message describing the result |
snapshotStatus |
SnapshotStatus | 'added' | 'matched' | 'updated' | 'failed' |
diffCount |
number | Number of different pixels (pixel-based methods) |
diffPercentage |
number | Percentage of different pixels |
score |
number | Similarity score (SSIM: 1 = identical, GMSD: 0 = identical) |
baselinePath |
string | Path to baseline snapshot |
receivedPath |
string | Path to received image (saved for debugging on failure) |
diffPath |
string | Path to diff visualization |
ImageInput
type ImageInput =
| string // File path
| Uint8Array // Encoded PNG bytes (a Buffer works)
| {
data: Uint8Array | Uint8ClampedArray | Buffer;
width: number;
height: number;
};
Comparison Methods
core-native
Rust-native comparison via N-API bindings. Fastest method with native performance.
- Input: File paths, PNG buffers or raw RGBA images
- Algorithm: YIQ color space with block-based optimization
- Best for: Production testing, large images
core-wasm
The core-native algorithm compiled to WebAssembly. Same results, no native binaries.
- Input: File paths or buffers
core
Pixel-by-pixel comparison in JavaScript. Pure JS implementation with no native dependencies.
- Input: File paths or buffers
- Algorithm: YIQ color space with anti-aliasing detection
- Best for: Cross-platform compatibility
ssim
Structural Similarity Index. Measures perceptual similarity.
- Input: File paths or buffers
- Algorithm: Standard SSIM with configurable window size
- Best for: Perceptual quality assessment
- Score: 1 = identical, lower = more different
msssim
Multi-Scale Structural Similarity Index.
- Input: File paths or buffers
- Algorithm: SSIM across multiple scales
- Best for: Images with varying resolutions or scales
hitchhikers-ssim
Fast SSIM approximation from Hitchhiker's Guide.
- Input: File paths or buffers
- Algorithm: Optimized SSIM calculation
- Best for: Faster SSIM with acceptable accuracy trade-off
gmsd
Gradient Magnitude Similarity Deviation.
- Input: File paths or buffers
- Algorithm: Gradient-based perceptual similarity
- Best for: Detecting structural changes
- Score: 0 = identical, higher = more different
Usage Examples
With File Paths
const result = await getOrCreateSnapshot(
'/path/to/screenshot.png',
{ method: 'core-native' },
{ testPath: __filename, testName: 'test name' }
);
With Image Buffers
const imageData = {
data: new Uint8Array([...]),
width: 800,
height: 600,
};
const result = await getOrCreateSnapshot(
imageData,
{ method: 'core' },
{ testPath: __filename, testName: 'test name' }
);
Custom Threshold
const result = await getOrCreateSnapshot(
imagePath,
{
method: 'core',
failureThreshold: 0.1,
failureThresholdType: 'percent', // Allow 0.1% difference
diff: { threshold: 0.05 }, // Stricter per-pixel color threshold
},
{ testPath: __filename, testName: 'test name' }
);
Different Comparison Methods
// Fastest - Rust native (paths, PNG buffers or raw images)
await getOrCreateSnapshot(imagePath, { method: 'core-native' }, context);
// Pure JavaScript
await getOrCreateSnapshot(imageBuffer, { method: 'core' }, context);
// Perceptual similarity
await getOrCreateSnapshot(imageBuffer, { method: 'ssim' }, context);
// Gradient-based
await getOrCreateSnapshot(imageBuffer, { method: 'gmsd' }, context);
Framework Integrations
This package provides the core logic for framework-specific integrations:
- @blazediff/jest - Jest matcher
- @blazediff/vitest - Vitest matcher
- @blazediff/bun - Bun test matcher