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

@blazediff/bun

toMatchImageSnapshot() for Bun. Auto-registers on import, supports all comparison methods.

Installation

npm install --save-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 page.screenshot(); await expect(screenshot).toMatchImageSnapshot({ method: 'core', snapshotIdentifier: 'homepage', }); });

The matcher auto-registers when you import @blazediff/bun. No additional setup required!

Unlike Jest/Vitest, Bun has limited context exposure. Always provide a snapshotIdentifier for reliable snapshot management.

API Reference

toMatchImageSnapshot(options?)

Bun test matcher for image snapshot comparison.

await expect(imageInput).toMatchImageSnapshot(options?);

Parameters

ParameterTypeDescription
imageInputImageInputImage to compare (file path or buffer with dimensions)
optionsPartial<MatcherOptions>Optional comparison options

Options

OptionTypeDefaultDescription
methodComparisonMethod'core'Comparison algorithm
snapshotIdentifierstring'snapshot'Required: Snapshot filename identifier
failureThresholdnumber0Allowed difference (pixels or percentage)
failureThresholdType'pixel' | 'percent''pixel'Threshold interpretation
snapshotsDirstring'__snapshots__'Snapshot directory (relative to test)
updateSnapshotsbooleanfalseForce snapshot update
thresholdnumber0.1Color threshold for core/core-native (0-1)
runInWorkerbooleantrueRun comparison in worker thread for better performance

See @blazediff/matcher for all available options.

Comparison Methods

core - Pure JavaScript (Default)

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

core-native - Rust Native (Fastest)

await expect('/path/to/image.png').toMatchImageSnapshot({ method: 'core-native', snapshotIdentifier: 'my-test', });

ssim - Perceptual Similarity

await expect(screenshot).toMatchImageSnapshot({ method: 'ssim', snapshotIdentifier: 'my-test', });

gmsd - Gradient-based

await expect(screenshot).toMatchImageSnapshot({ method: 'gmsd', snapshotIdentifier: 'my-test', });

Usage Patterns

Basic Snapshot Test

import { expect, it } from 'bun:test'; import '@blazediff/bun'; it('renders homepage correctly', async () => { const screenshot = await browser.screenshot(); await expect(screenshot).toMatchImageSnapshot({ method: 'core', snapshotIdentifier: 'homepage', }); });

Custom Thresholds

Allow small differences while catching regressions:

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

Use percentage-based thresholds for responsive images that may change size across different viewports.

Update Snapshots

# Update all failing snapshots (recommended) bun test --update-snapshots # Using environment variable BUN_UPDATE_SNAPSHOTS=true bun test

The Bun’s --update-snapshots flag is consumed by Bun internally and won’t update image snapshots.

Programmatically:

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

Custom Snapshot Directory

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

Bun-Specific Notes

Unlike Jest and Vitest, Bun has limited test context exposure. Always provide a snapshotIdentifier:

// Good await expect(screenshot).toMatchImageSnapshot({ method: 'core', snapshotIdentifier: 'my-component', }); // Avoid - falls back to generic "snapshot" await expect(screenshot).toMatchImageSnapshot({ method: 'core', });
Last updated on