Add macOS snapshot test support - #168
Merged
Merged
Conversation
BarredEwe
commented
Aug 30, 2026
BarredEwe
left a comment
Owner
Author
There was a problem hiding this comment.
Earlier review items are addressed in later commits on this branch. See the resolved threads for the template, wrapper names, window host, PreviewProvider layout, docs, and image assertions.
BarredEwe
force-pushed
the
codex/macos-snapshot-tests
branch
3 times, most recently
from
August 31, 2026 09:15
75fb3f0 to
18fb4e2
Compare
Restore a compiling PrefireProvider template, reuse ViewRepresentable names on AppKit, map PreviewLayout.fixed into DeviceConfig, and host SwiftUI in an NSWindow so SnapshotTesting's NSView.image path can run.
orderFrontRegardless was flashing a window for every snapshot. The hosting NSWindow stays attached for layout, but is never ordered front.
BarredEwe
force-pushed
the
codex/macos-snapshot-tests
branch
from
August 31, 2026 18:34
18fb4e2 to
1e1c8ab
Compare
BarredEwe
marked this pull request as ready for review
August 31, 2026 18:35
Three issues in the macOS hosting path: - Content larger than the 4096 pt canvas failed the size check and fell back to 1x1, producing an image that records and re-verifies while showing nothing. Clamp it instead. - The container stored its NSWindow while the window retains its content view, so each snapshot kept a window and its SwiftUI tree alive. Host in a shared off-screen window, keyed by scale. - backingScaleFactor came from the current display, so the same test recorded @2x on a Retina Mac and @1x elsewhere. Fix it at 2 by default, configurable through DeviceConfig(size:scale:). .fixedLayout and .previewLayout(.fixed) now apply on macOS only; applying them on iOS and tvOS changes the size of existing reference images. DeviceConfig moves out of #if canImport(XCTest). PreviewModel stores one unconditionally, so the gate broke compilation wherever XCTest cannot be imported. PrefireNativeView and PrefireNativeViewController replace the duplicated per-platform initialisers. The iOS-only traits: variants remain as overloads, so existing call sites still compile.
- Extract traitArguments(from:), shared by trait and fixed-layout parsing. - Collapse fixedLayoutWidth, fixedLayoutHeight and fixedLayoutSize into a single stencil key; only fixedLayoutSize was used by the template. - Emit one verifySnapshot call, with the platform selecting only the Snapshotting strategy. Drops the snapshotLayout helper and restores the original iOS layout expression. - Remove test_defaultTemplateContainsMacOSSnapshotBranch: containment assertions over the template text, including negative ones for helpers that do not exist. PrefireGeneratorTests already cover the rendered output. Bundled binary rebuilt with make binary.
…ation The dependency backed a single test that built a Snapshotting value and discarded it. Generated code is not compiled by CI on any platform, so that test did not cover the macOS template branch either. Removed together with Package.resolved. Documentation: - macOS supports snapshot tests only; PlaybookView is iOS-only. - Every macOS preview renders at its fitting size, not just components. - --disable-sandbox is needed for recording, because SwiftPM's sandbox denies writes into the package directory. Verifying works with a plain swift test, which CI now uses. - Requirements listed Swift 5.6 and Xcode 14 while Package.swift declares swift-tools-version 6.0. Makefile: 'generic/platform=iOS Simulator,OS=latest' resolves OS against the selected Xcode's SDK version and fails when that version is not installed. Drop the pin and the redundant -sdk, make the destination overridable, and report the missing-platform case on failure.
BarredEwe
force-pushed
the
codex/macos-snapshot-tests
branch
from
August 31, 2026 19:36
1e1c8ab to
437a2af
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds macOS 13+ snapshot-test support for SwiftUI,
NSView, andNSViewController, without changing iOS behaviour.Problem
Package.swiftalready declared macOS support, but the library imported UIKit unconditionally. This prevented the package from compiling on macOS, and generated snapshot tests contained UIKit, simulator, and iOS-device assumptions.Changes
macOS support
PreviewModel/PrefireSnapshotoverloads forNSViewandNSViewController.NSWindow; SnapshotTesting'sNSView.imagestrategy captures the image..fixedLayout(width:height:)and.previewLayout(.fixed(width:height:))pin the macOS canvas. Applied on macOS only — honouring them on iOS would resize every already recorded reference image.DeviceConfig(size:scale:)) instead of being inherited from the current display, so a snapshot recorded on a Retina Mac matches one verified on CI.NSWindowplus a full SwiftUI tree per snapshot.Cleanups
PrefireNativeView/PrefireNativeViewControllercollapse the duplicated per-platform initialisers inPrefireSnapshotandPreviewModel; the iOS-onlytraits:variants remain as overloads, so existing call sites keep compiling.verifySnapshotcall — the platform only selects theSnapshottingstrategy.traitArguments(from:)is shared by trait and fixed-layout parsing; the three fixed-layout stencil keys collapse into onefixedLayoutSize.DeviceConfigmoves out of#if canImport(XCTest):PreviewModelstores one unconditionally, so the gate made the library fail to compile wherever XCTest is not importable.Build
swift-snapshot-testingis not required by this package.make buildno longer pinsOS=latest, which resolves against the selected Xcode's SDK version and fails when that exact version is not installed. The destination is generic and overridable viaDESTINATION=.Notes
NSViewRepresentableandNSViewControllerRepresentableare SwiftUI protocol names, so the public wrappers reuse the iOS namesViewRepresentableandViewControllerRepresentable.macOS supports snapshot tests only —
PlaybookViewremains iOS-only, and the README says so.Validation
swift test(root package) — 17 testscd PrefireExecutable && swift test— 56 testsswiftc -typecheckofSources/Prefirefor iOS, tvOS and macOS — cleanmake binary