Visual Regression Testing & Deterministic Rasterization
Screenshot diffs generated on macOS Retina fail in Linux CI because CoreText and FreeType rasterize subpixel glyph edges differently; pinning container rasterizers and fonts eliminates false diffs.
About this page
Deterministic Rasterization for Visual Snapshots
Trace how OS font hinting and async web fonts cause false diffs, and how containerization stabilizes pixels.
macOS CoreText and Linux FreeType rasterize anti-aliased glyph edges differently, causing false diffs.
// macOS Retina DPR=2 vs Linux CI DPR=1 produces 3.2% pixel diff- os
- macOS vs Linux
- dpr
- 2 vs 1
- diffRatio
- 0.032
Under the Hood#
Blink's Skia backend delegates font shaping and glyph rasterization to the host OS text stack unless constrained by a uniform container environment.
- OS Font Hinting Divergence: macOS CoreText applies grayscale anti-aliasing at
devicePixelRatio: 2, whereas Linux FreeType applies different hinting and LCD subpixel filtering atdevicePixelRatio: 1. - Font & Image Readiness: Capturing a frame before
await document.fonts.readyor image decode causes FOUT fallback diffs across runs. - Animation & Caret Freezing: Passing
animations: 'disabled'andreducedMotion: 'reduce'(prefers-reduced-motion) fast-forwards CSS transitions to their final keyframe and hides blinking text carets.
await page.emulateMedia({ reducedMotion: 'reduce' });
await page.evaluate(() => document.fonts.ready);
await expect(page.getByTestId('pricing-card')).toHaveScreenshot({
animations: 'disabled',
maxDiffPixelRatio: 0.005,
});Problem Genealogy#
- 2014 (PhantomJS / Host-OS Screenshots): Teams committed macOS baselines that failed 100% of Linux CI builds due to 1px-2px font baseline shifts.
- 2018 (High
threshold: 0.2Workarounds): Raising pixel tolerance to silence font noise let real 2px border regressions and broken icons pass undetected. - 2022 (Pinned Docker Chromium +
animations: 'disabled'): Standardizing on identical Linux container images cut false-positive visual diffs below 0.5% while preserving strict pixel thresholds.
Production Pitfalls#
- Non-Deterministic
<video>and WebGL Frames: Hardware-accelerated<canvas>and<video>elements decode at variable frame timestamps and GPU driver tolerances across runs. - Dynamic Timestamps & Avatars: Unmasked relative times (
2 minutes ago) or external CDN images trigger constant pixel churn unless stubbed or passed inmask: [].
Where this connects
Prerequisites
Device Pixel Ratio & Subpixel Rendering
Device pixel ratio and subpixel anti-aliasing alter edge pixel luminance across host GPUs.
Font Loading & CLS Mechanics
Capturing screenshots before document.fonts.ready produces fallback-font diffs and layout shifts.
User Preference Media Queries
Forcing prefers-reduced-motion disables mid-frame CSS transitions during screenshot capture.