Skip to Content
New: codec-png-native and codec-png-wasm bring the blazediff-png codec to Node and WebAssembly, faster than spng and jSquash. Read more β†’
APIs@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: a file path, encoded PNG bytes, or raw RGBA pixels 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 options sit in one namespace per method family. The matcher manages output itself:

OptionTypeUsed byDescription
diffOmit<DiffOptions, "output">core, core-wasm, core-nativethreshold, antialiasing (default true), metric, mask, window, render. See @blazediff/core
ssimOmit<SsimOptions, "output">ssim, msssim, hitchhikers-ssimwindowSize, k1, k2, bitDepth, msSsim, hitchhikers. See @blazediff/ssim
gmsdOmit<GmsdOptions, "output">gmsddownsample, c. See @blazediff/gmsd
runInWorkerbooleanallRun image I/O and comparison in a worker thread for better performance. Default: true
await getOrCreateSnapshot(screenshot, { method: 'core', failureThreshold: 0.1, failureThresholdType: 'percent', diff: { threshold: 0.05, metric: 'oklab' }, }, testContext);

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 | Uint8Array // Encoded PNG bytes (a Buffer works) | { data: Uint8Array | Uint8ClampedArray | Buffer; width: number; height: number; };

Comparison Methods

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

Last updated on