@blazediff/bun

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

Bun test matcher for visual regression testing with blazediff

原始使用文档
文档目录23 节
  1. Features
  2. Installation
  3. Quick Start
  4. API Reference
  5. toMatchImageSnapshot(options?)
  6. Usage Patterns
  7. Basic Snapshot Test
  8. Different Comparison Methods
  9. Update Snapshots
  10. Custom Thresholds
  11. Custom Snapshot Directory
  12. With Playwright
  13. Negation
  14. Serial Tests
  15. Snapshot State Tracking
  16. Bun-Specific Notes
  17. Snapshot Identifier Required
  18. Test Path Detection
  19. Setup
  20. Auto-setup (Recommended)
  21. Manual Setup
  22. TypeScript
  23. Links

@blazediff/bun

npm bundle size NPM Downloads

Bun test matcher for visual regression testing with blazediff. Powered by @blazediff/matcher with Bun-specific snapshot state integration.

Features

  • Native Bun matcher: toMatchImageSnapshot() extends Bun's expect
  • Snapshot state tracking: Bun reports accurate snapshot counts (when API available)
  • Multiple comparison algorithms: core, core-native, ssim, msssim, hitchhikers-ssim, gmsd
  • Auto-setup: Imports and registers automatically
  • Update mode: Works with Bun's -u/--update flag
  • TypeScript support: Full type definitions included

Installation

npm install --dev @blazediff/bun

Peer dependencies: Bun >= 1.0.0

Quick Start

import { expect, it } from 'bun:test';
import '@blazediff/bun';

it('should match screenshot', async () => {
  const screenshot = await takeScreenshot();

  await expect(screenshot).toMatchImageSnapshot({
    method: 'core',
  });
});

API Reference

toMatchImageSnapshot(options?)

Bun test matcher for image snapshot comparison.

Parameter Type Description
options Partial<MatcherOptions> Optional comparison options (see below)

Options

Option Type Default Description
method 'core' | 'core-native' | 'ssim' | 'msssim' | 'hitchhikers-ssim' | 'gmsd' 'core' 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 'snapshot' Identifier for the snapshot file (required for Bun)
updateSnapshots boolean false Force update snapshots
diff object - Options for core, core-wasm and core-native, such as { threshold: 0.1, antialiasing: true }. ssim and gmsd hold the options of those methods. See @blazediff/matcher
runInWorker boolean true Run comparison in worker thread for better performance

See @blazediff/matcher for full options documentation.

Usage Patterns

Basic Snapshot Test

import { expect, it } from 'bun:test';
import '@blazediff/bun';

it('renders correctly', async () => {
  const screenshot = await page.screenshot();

  await expect(screenshot).toMatchImageSnapshot({
    method: 'core',
    snapshotIdentifier: 'homepage',
  });
});

Different Comparison Methods

// Fast Rust-native comparison (paths, PNG buffers or raw images)
await expect('/path/to/image.png').toMatchImageSnapshot({
  method: 'core-native',
  snapshotIdentifier: 'image-core-native',
});

// Pure JavaScript comparison
await expect(imageBuffer).toMatchImageSnapshot({
  method: 'core',
  snapshotIdentifier: 'image-core',
});

// Perceptual similarity (SSIM)
await expect(imageBuffer).toMatchImageSnapshot({
  method: 'ssim',
  snapshotIdentifier: 'image-ssim',
});

// Gradient-based comparison
await expect(imageBuffer).toMatchImageSnapshot({
  method: 'gmsd',
  snapshotIdentifier: 'image-gmsd',
});

Update Snapshots

# Update all snapshots (recommended)
bun test --update-snapshots

# Or using environment variable
BUN_UPDATE_SNAPSHOTS=true bun test

Or programmatically:

await expect(screenshot).toMatchImageSnapshot({
  method: 'core',
  snapshotIdentifier: 'homepage',
  updateSnapshots: true,
});

Custom Thresholds

// Allow up to 100 pixels difference
await expect(screenshot).toMatchImageSnapshot({
  method: 'core',
  snapshotIdentifier: 'homepage',
  failureThreshold: 100,
  failureThresholdType: 'pixel',
});

// Allow up to 0.1% difference
await expect(screenshot).toMatchImageSnapshot({
  method: 'core',
  snapshotIdentifier: 'homepage',
  failureThreshold: 0.1,
  failureThresholdType: 'percent',
});

Custom Snapshot Directory

await expect(screenshot).toMatchImageSnapshot({
  method: 'core',
  snapshotIdentifier: 'homepage',
  snapshotsDir: '__image_snapshots__',
});

With Playwright

import { test, expect } from 'bun:test';
import '@blazediff/bun';
import { chromium } from 'playwright';

test('visual regression with Playwright', async () => {
  const browser = await chromium.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');

  const screenshot = await page.screenshot();
  await expect(screenshot).toMatchImageSnapshot({
    method: 'core',
    snapshotIdentifier: 'homepage',
  });

  await browser.close();
});

Negation

// Assert images are different
await expect(screenshot).not.toMatchImageSnapshot({
  method: 'core',
  snapshotIdentifier: 'different',
});

Serial Tests

For tests that might interfere with each other (e.g., cleaning up snapshots), use serial execution:

import { describe, it } from 'bun:test';

describe.serial('Visual regression tests', () => {
  it('test 1', async () => {
    await expect(image1).toMatchImageSnapshot({
      method: 'core',
      snapshotIdentifier: 'test-1',
    });
  });

  it('test 2', async () => {
    await expect(image2).toMatchImageSnapshot({
      method: 'core',
      snapshotIdentifier: 'test-2',
    });
  });
});

Snapshot State Tracking

This matcher attempts to integrate with Bun's snapshot state tracking system. If Bun exposes the snapshot state API to custom matchers, test summaries will show accurate counts:

Snapshots  2 written | 1 updated | 5 passed

Note: Bun's snapshot state API for custom matchers is limited. If snapshot state is unavailable, image snapshots will still work correctly but may not appear in Bun's test summary counts.

Bun-Specific Notes

Snapshot Identifier Required

Unlike Jest and Vitest which can auto-generate snapshot identifiers from test names, Bun has limited context exposure. It's recommended to always provide a snapshotIdentifier:

await expect(screenshot).toMatchImageSnapshot({
  method: 'core',
  snapshotIdentifier: 'my-component-state', // Recommended
});

Test Path Detection

Bun provides Bun.main which is used to determine the test file path. In some edge cases, you may need to specify snapshotsDir as an absolute path:

await expect(screenshot).toMatchImageSnapshot({
  method: 'core',
  snapshotIdentifier: 'homepage',
  snapshotsDir: '/absolute/path/to/snapshots',
});

Setup

Simply import the package in your test file:

import '@blazediff/bun';

The matcher is automatically registered when imported.

Manual Setup

Alternatively, call the setup function explicitly:

import { setupBlazediffMatchers } from '@blazediff/bun';

setupBlazediffMatchers();

TypeScript

TypeScript types are included. To use the matcher with TypeScript:

import '@blazediff/bun';

declare module 'bun:test' {
  interface Matchers<T = unknown> {
    toMatchImageSnapshot(options?: Partial<import('@blazediff/matcher').MatcherOptions>): Promise<MatcherResult>;
  }
}

The type augmentation is automatically included when you import the package.

版本与发布记录

@blazediff/bun 共发布 31 个版本,首次发布于 2026/01/05;过去 12 个月发布了 31 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.0.0latest2026/11/053 天前
1.2.162026/10/2612 天前
1.2.152026/08/272 个月前
1.2.142026/08/172 个月前
1.2.132026/08/033 个月前
1.2.122026/07/163 个月前

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

关于 @blazediff/bun 的常见问题

@blazediff/bun 是什么?

Bun test matcher for visual regression testing with blazediff

如何安装 @blazediff/bun?

在项目目录运行 npm install @blazediff/bun,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:bun(>=1.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@blazediff/bun 支持 TypeScript 吗?

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

@blazediff/bun 支持 ESM 还是 CommonJS?

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

@blazediff/bun 可以商用吗?

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

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

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

@blazediff/bun 还在维护吗?

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