Skip to content

alexey1312/SnapshotTestingWebP

Repository files navigation

SnapshotTestingWebP

Swift Platforms License

⚠️ Experimental Development: This library is currently in experimental development stage.

A Swift Package Manager library that extends PointFree's SnapshotTesting framework to support WebP image format for snapshot tests.

WebP provides excellent compression with high image quality, resulting in significantly smaller snapshot files while maintaining the same testing capabilities as PNG snapshots.

Features

  • 📸 WebP Snapshot Testing - Full support for WebP format in snapshot tests
  • 🗜️ Advanced Compression - Multiple compression quality levels from lossless to maximum compression
  • Optimized Performance - Hardware-accelerated encoding with ~75-80% faster performance
  • 🎯 Cross-Platform - iOS 13+, macOS 10.15+, tvOS 13+ support
  • 🔧 Drop-in Replacement - Easy migration from PNG snapshots
  • 📱 Complete UI Coverage - UIView, UIViewController, NSView, NSViewController, SwiftUI support

Installation

Swift Package Manager

Add the following to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/YourUsername/SnapshotTestingWebP.git", from: "1.0.0")
]

Or add it through Xcode: File > Swift Packages > Add Package Dependency

Quick Start

import XCTest
import SnapshotTesting
import SnapshotTestingWebP

class MyViewTests: XCTestCase {
    func testMyView() {
        let view = MyView()

        // WebP snapshot with lossless compression
        assertSnapshot(of: view, as: .imageWebP)

        // WebP with custom compression quality
        assertSnapshot(of: view, as: .imageWebP(compressionQuality: .medium))
    }
}

Supported Types

UIKit (iOS/tvOS)

// UIView snapshots
assertSnapshot(of: myView, as: .imageWebP)

// UIViewController snapshots
assertSnapshot(of: myViewController, as: .imageWebP(on: .iPhone13))

// SwiftUI View snapshots
assertSnapshot(of: MySwiftUIView(), as: .imageWebP(layout: .sizeThatFits))

AppKit (macOS)

// NSView snapshots
assertSnapshot(of: myNSView, as: .imageWebP)

// NSViewController snapshots
assertSnapshot(of: myNSViewController, as: .imageWebP)

Compression Quality

The library provides several compression quality options:

public enum CompressionQuality {
    case lossless    // Perfect quality, larger file size
    case low         // 80% quality
    case medium      // 50% quality
    case high        // 20% quality
    case maximum     // 0% quality, smallest file size
    case custom(CGFloat) // Custom quality (0.0 - 1.0)
}

Usage Examples

// Lossless compression (default)
assertSnapshot(of: view, as: .imageWebP(compressionQuality: .lossless))

// Medium compression for balanced size/quality
assertSnapshot(of: view, as: .imageWebP(compressionQuality: .medium))

// Custom compression level
assertSnapshot(of: view, as: .imageWebP(compressionQuality: .custom(0.85)))

Advanced Configuration

Precision Control

assertSnapshot(
    of: view,
    as: .imageWebP(
        precision: 0.98, // 98% pixel match required
        perceptualPrecision: 0.99, // 99% perceptual precision
        compressionQuality: .medium
    )
)

SwiftUI Layouts

// Device-specific layout
assertSnapshot(
    of: MySwiftUIView(),
    as: .imageWebP(layout: .device(config: .iPhone13))
)

// Fixed size layout
assertSnapshot(
    of: MySwiftUIView(),
    as: .imageWebP(layout: .fixed(width: 300, height: 200))
)

// Size that fits content
assertSnapshot(
    of: MySwiftUIView(),
    as: .imageWebP(layout: .sizeThatFits)
)

Performance

WebP encoding in this library is highly optimized:

  • Hardware-accelerated preprocessing using vImage framework
  • Advanced libwebp configuration with optimized presets
  • Multi-threaded encoding for improved performance
  • Smart image scaling with optimal dimension limits

Performance Results:

  • ~75-80% faster encoding compared to basic libwebp implementation
  • Encoding time reduced from ~0.3s to ~0.08s for typical 400x300 images
  • Maintains high image quality while significantly improving speed

File Size Benefits

WebP typically provides:

  • 25-35% smaller file sizes compared to PNG
  • Better compression than JPEG with transparency support
  • Lossless mode for pixel-perfect accuracy when needed

Migration from PNG

Replace your existing PNG snapshots:

// Before (PNG)
assertSnapshot(of: view, as: .image)

// After (WebP)
assertSnapshot(of: view, as: .imageWebP)

The API is designed to be a drop-in replacement for existing snapshot strategies.

Requirements

  • iOS 13.0+ / macOS 10.15+ / tvOS 13.0+
  • Swift 5.2+
  • Xcode 11.0+

Dependencies

License

This library is released under the MIT License. See LICENSE for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Known Issues

  • Non-deterministic snapshots: Snapshots may produce different results on each test run, causing tests to fail unexpectedly. This issue is being actively investigated.
  • Slow test performance: Tests may run slower than expected due to WebP encoding complexity and hardware acceleration initialization.

Related Projects

About

An extension to SnapshotTesting which allows you to create WebP images

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published