Core matcher logic for visual regression testing with blazediff
npm install @blazediff/matcher

Core matcher logic for visual regression testing. Provides snapshot comparison with multiple algorithms, framework-agnostic APIs, and snapshot state tracking.
- Multiple comparison methods: core, bin, 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
``bash`
npm install @blazediff/matcher
`typescript
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
`
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
| 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) |
threshold | number | 0.1 | Color difference threshold for core/bin methods (0-1) |
antialiasing | boolean | false | Enable anti-aliasing detection (bin method) |
includeAA | boolean | false | Include anti-aliased pixels in diff count (core method) |
windowSize | number | 11 | Window size for SSIM variants |
k1 | number | 0.01 | k1 constant for SSIM |
k2 | number | 0.03 | k2 constant for SSIM |
downsample | 0 | 1 | 0 | Downsample factor for GMSD |
| 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 |
`typescript`
type ImageInput =
| string // File path
| {
data: Uint8Array | Uint8ClampedArray | Buffer;
width: number;
height: number;
};
`typescript`
const result = await getOrCreateSnapshot(
'/path/to/screenshot.png',
{ method: 'bin' },
{ testPath: __filename, testName: 'test name' }
);
`typescript
const imageData = {
data: new Uint8Array([...]),
width: 800,
height: 600,
};
const result = await getOrCreateSnapshot(
imageData,
{ method: 'core' },
{ testPath: __filename, testName: 'test name' }
);
`
`typescript`
const result = await getOrCreateSnapshot(
imagePath,
{
method: 'core',
failureThreshold: 0.1,
failureThresholdType: 'percent', // Allow 0.1% difference
},
{ testPath: __filename, testName: 'test name' }
);
`typescript
// Fastest - Rust native (file paths only)
await getOrCreateSnapshot(imagePath, { method: 'bin' }, 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);
``
This package provides the core logic for framework-specific integrations:
- @blazediff/jest - Jest matcher
- @blazediff/vitest - Vitest matcher
- @blazediff/bun - Bun test matcher