@blazediff/bun
toMatchImageSnapshot() for Bun. Auto-registers on import, supports all comparison methods.
Installation
npm install --save-dev @blazediff/bunPeer 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
| Parameter | Type | Description |
|---|---|---|
imageInput | ImageInput | Image to compare (file path or buffer with dimensions) |
options | Partial<MatcherOptions> | Optional comparison options |
Options
| Option | Type | Default | Description |
|---|---|---|---|
method | ComparisonMethod | 'core' | Comparison algorithm |
snapshotIdentifier | string | 'snapshot' | Required: Snapshot filename identifier |
failureThreshold | number | 0 | Allowed difference (pixels or percentage) |
failureThresholdType | 'pixel' | 'percent' | 'pixel' | Threshold interpretation |
snapshotsDir | string | '__snapshots__' | Snapshot directory (relative to test) |
updateSnapshots | boolean | false | Force snapshot update |
threshold | number | 0.1 | Color threshold for core/core-native (0-1) |
runInWorker | boolean | true | Run 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 testThe 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',
});Links
- GitHub Repository
- NPM Package
- @blazediff/matcher - Core matcher logic
- Bun Documentation