@blazediff/matcher

中文文档
3.0.0发布于 2026/11/05MITTypeScriptESM + CJS构建来源已验证近 30 天下载 1.3万

Core matcher logic for visual regression testing with blazediff

原始使用文档
文档目录23 节
  1. Features
  2. Installation
  3. Quick Start
  4. API Reference
  5. getOrCreateSnapshot(received, options, testContext)
  6. MatcherOptions
  7. ComparisonResult
  8. ImageInput
  9. Comparison Methods
  10. core-native
  11. core-wasm
  12. core
  13. ssim
  14. msssim
  15. hitchhikers-ssim
  16. gmsd
  17. Usage Examples
  18. With File Paths
  19. With Image Buffers
  20. Custom Threshold
  21. Different Comparison Methods
  22. Framework Integrations
  23. Links

@blazediff/matcher

npm bundle size NPM Downloads

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/matcher 共发布 31 个版本,首次发布于 2026/01/05;过去 12 个月发布了 31 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.0.0latest2026/11/053 天前
2.0.82026/10/2612 天前
2.0.72026/08/272 个月前
2.0.62026/08/172 个月前
2.0.52026/08/033 个月前
2.0.42026/07/163 个月前

其他发布通道:fix-testing-matcher-performance → 0.0.0-fix-testing-matcher-performance-20260223115318。

关于 @blazediff/matcher 的常见问题

@blazediff/matcher 是什么?

Core matcher logic for visual regression testing with blazediff

如何安装 @blazediff/matcher?

在项目目录运行 npm install @blazediff/matcher,或使用 pnpm add / yarn add / bun add。

@blazediff/matcher 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@blazediff/matcher 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@blazediff/matcher 可以商用吗?

@blazediff/matcher 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@blazediff/matcher 的下载量有多少?

根据 npm 官方下载统计,@blazediff/matcher 在 2026/10/09 – 2026/11/07 这 30 天内被下载 13,125 次,日均约 438 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@blazediff/matcher 还在维护吗?

最新版本 3.0.0 发布于 2026/11/05(3 天前);至今共发布 31 个版本,其中过去 12 个月发布了 31 个;首次发布于 2026/01/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。