⚠️ 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.
- 📸 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
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
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))
}
}
// 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))
// NSView snapshots
assertSnapshot(of: myNSView, as: .imageWebP)
// NSViewController snapshots
assertSnapshot(of: myNSViewController, as: .imageWebP)
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)
}
// 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)))
assertSnapshot(
of: view,
as: .imageWebP(
precision: 0.98, // 98% pixel match required
perceptualPrecision: 0.99, // 99% perceptual precision
compressionQuality: .medium
)
)
// 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)
)
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
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
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.
- iOS 13.0+ / macOS 10.15+ / tvOS 13.0+
- Swift 5.2+
- Xcode 11.0+
- swift-snapshot-testing (1.18.6+)
- libwebp (1.4.1+)
This library is released under the MIT License. See LICENSE for details.
Contributions are welcome! Please feel free to submit a Pull Request.
- 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.
- SnapshotTestingHEIC - HEIC format support
- swift-snapshot-testing - The original snapshot testing framework