Skip to Content
New: @blazediff/core-native now includes interpret — structured diff analysis to understand what changed. Read more →
Documentation@blazediff/matcher

@blazediff/matcher

Core snapshot comparison logic that powers @blazediff/jest, @blazediff/vitest, and @blazediff/bun. Most users should use those framework packages directly.

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', } ); if (result.pass) { console.log(`✓ Snapshot ${result.snapshotStatus}`); } else { console.log(`✗ ${result.diffPercentage}% different`); }

API Reference

getOrCreateSnapshot(received, options, testContext)

Main function for snapshot comparison and management.

ParameterTypeDescription
receivedImageInputImage to compare (file path or buffer with dimensions)
optionsMatcherOptionsComparison options
testContextTestContextTest information (testPath, testName)

Returns: Promise<ComparisonResult>

MatcherOptions

Core comparison options:

OptionTypeDefaultDescription
methodComparisonMethod-Comparison algorithm: 'core', 'core-native', 'ssim', 'msssim', 'hitchhikers-ssim', 'gmsd'
failureThresholdnumber0Number of pixels or percentage difference allowed
failureThresholdType'pixel' | 'percent''pixel'How to interpret failureThreshold
snapshotsDirstring'__snapshots__'Directory to store snapshots (relative to test file)
snapshotIdentifierstringauto-generatedCustom identifier for the snapshot file
updateSnapshotsbooleanfalseForce update snapshots

Method-specific options:

OptionTypeDefaultDescription
thresholdnumber0.1Color difference threshold (0-1) for core/core-native methods
antialiasingbooleanfalseEnable anti-aliasing detection (core-native method)
includeAAbooleanfalseInclude anti-aliased pixels in diff count (core method)
windowSizenumber11Window size for SSIM variants
k1number0.01k1 constant for SSIM
k2number0.03k2 constant for SSIM
downsample0 | 10Downsample factor for GMSD
runInWorkerbooleantrueRun image I/O and comparison in a worker thread for better performance

ComparisonResult

Result object returned by getOrCreateSnapshot:

FieldTypeDescription
passbooleanWhether the comparison passed
messagestringHuman-readable message describing the result
snapshotStatusSnapshotStatusStatus: 'added', 'matched', 'updated', 'failed'
diffCountnumber?Number of different pixels (pixel-based methods)
diffPercentagenumber?Percentage of different pixels
scorenumber?Similarity score (SSIM: 1 = identical, GMSD: 0 = identical)
baselinePathstring?Path to baseline snapshot
receivedPathstring?Path to received image (saved on failure)
diffPathstring?Path to diff visualization

ImageInput

type ImageInput = | string // File path | { data: Uint8Array | Uint8ClampedArray | Buffer; width: number; height: number; };

Comparison Methods

Available methods: core, core-native, ssim, msssim, hitchhikers-ssim, gmsd. See @blazediff/core, @blazediff/core-native, @blazediff/ssim, and @blazediff/gmsd for algorithm details.

Last updated on