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

@blazediff/jest

toMatchImageSnapshot() for Jest. Auto-registers on import, tracks snapshot state, supports all comparison methods.

Installation

npm install --save-dev @blazediff/jest

Peer dependencies: Jest >= 27.0.0

Quick Start

import '@blazediff/jest'; describe('Visual Regression Tests', () => { it('should match screenshot', async () => { const screenshot = await page.screenshot(); await expect(screenshot).toMatchImageSnapshot({ method: 'core', }); }); });

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

API Reference

toMatchImageSnapshot(options?)

Jest 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
failureThresholdnumber0Allowed difference (pixels or percentage)
failureThresholdType'pixel' | 'percent''pixel'Threshold interpretation
snapshotsDirstring'__snapshots__'Snapshot directory (relative to test)
snapshotIdentifierstringauto-generatedCustom snapshot filename
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', // Fast, works with buffers and file paths });

core-native - Rust Native (Fastest)

await expect('/path/to/image.png').toMatchImageSnapshot({ method: 'core-native', // Requires file paths });

ssim - Perceptual Similarity

await expect(screenshot).toMatchImageSnapshot({ method: 'ssim', // Structural similarity });

gmsd - Gradient-based

await expect(screenshot).toMatchImageSnapshot({ method: 'gmsd', // Detects structural changes });

Usage Patterns

Basic Snapshot Test

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

Custom Thresholds

Allow small differences while catching regressions:

// Allow up to 100 pixels difference await expect(screenshot).toMatchImageSnapshot({ method: 'core', failureThreshold: 100, failureThresholdType: 'pixel', }); // Allow up to 0.5% difference await expect(screenshot).toMatchImageSnapshot({ method: 'core', 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 jest -u # Update snapshots for specific test file jest -u src/components/Button.test.tsx # Update snapshots matching pattern jest -u --testNamePattern="renders correctly" # Using environment variable JEST_UPDATE_SNAPSHOTS=true jest

Programmatically:

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

Custom Snapshot Directory

await expect(screenshot).toMatchImageSnapshot({ method: 'core', snapshotsDir: '__image_snapshots__', // Custom directory });

Custom Snapshot Identifier

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

Negation

Test that images are intentionally different:

const before = await page.screenshot(); await page.click('.toggle-theme'); const after = await page.screenshot(); // Assert that theme toggle changed the UI await expect(after).not.toMatchImageSnapshot({ method: 'core', snapshotIdentifier: 'before-theme-toggle', });

Configuration

Global Setup

To avoid importing in every test file, configure Jest:

// jest.config.js module.exports = { setupFilesAfterEnv: ['<rootDir>/jest.setup.js'], };
// jest.setup.js import '@blazediff/jest';
Last updated on