swift-snapshot-testing icon indicating copy to clipboard operation
swift-snapshot-testing copied to clipboard

Xcode 16 - SnapshotTesting in a Swift Package

Open kelvinlauKL opened this issue 1 year ago • 4 comments

I'm building a UI library as a Swift Package. Attempting to use SnapshotTesting gives me this:

Screenshot 2024-07-28 at 8 38 23 PM

Here's my package file:

// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
  name: "ChatDesignSystem",
  platforms: [.macOS(.v15), .iOS(.v18), .tvOS(.v18), .watchOS(.v11), .macCatalyst(.v18)],
  products: [
    .library(
      name: "ChatDesignSystem",
      targets: ["ChatDesignSystem"]),
  ],
  dependencies: [
    .package(url: "https://github.com/pointfreeco/swift-snapshot-testing", branch: "main")
  ],
  targets: [
    .target(
      name: "ChatDesignSystem"),
    .testTarget(
      name: "ChatDesignSystemTests",
      dependencies: [
        "ChatDesignSystem",
        .product(name: "SnapshotTesting", package: "swift-snapshot-testing")
      ]
    ),
  ]
)

Is this not supported in a Swift Package?

kelvinlauKL avatar Jul 29 '24 00:07 kelvinlauKL

@kelvinlauKL The screenshot has the error message mostly truncated. It looks like a compile failure but it's not easy to troubleshoot as is. Can you share sample code reproducing the issue?

stephencelis avatar Jul 29 '24 03:07 stephencelis

My bad - the error message is Type of expression is ambiguous without a type annotation. I realized this compiler error is happening when my device is set to "My Mac". The compiler error goes away if I set it to an iOS device.

I've updated the screenshot.

Is that expected behaviour?

kelvinlauKL avatar Jul 29 '24 03:07 kelvinlauKL

I have the same issue and was looking into it. I found out to my surprise that the current library does not seam to have the image not compiled for MacOS as the current snapshot implementation seams to rely on making the view a UIImage and embedding it in a viewController. Both do not exist on macOS.

So SwiftUI view support is limited to iOS like platforms Screenshot 2024-08-05 at 14 45 20

doozMen avatar Aug 05 '24 12:08 doozMen

Bummer! It would be great to have similar support for MacOS

gsabran avatar Feb 25 '25 20:02 gsabran

Hey @stephencelis! Is there a reason it's not available for mac? I've been working on the design system for my mac apps and decided to use snapshotting to verify and showcase implemented components and hit the same problem as other guys from the thread.

iSapozhnik avatar Aug 01 '25 15:08 iSapozhnik

@iSapozhnik, @gsabran, @doozMen: The library is extensible so that you can add your own snapshot strategies if the ones in the library do not suite your needs. You will see that in the screenshot above, the strategy is defined as:

extension Snapshotting where Value: SwiftUI.View, Format == UIImage {
  …
}

So, you will want to try to make an equivalent for NSImage:

extension Snapshotting where Value: SwiftUI.View, Format == NSImage {
  …
}

Give that a shot and let us know how it goes.

This is not considered an issue with the library currently, and so I am going to convert this to a discussion. Feel free to keep the conversation going over there.

mbrandonw avatar Aug 01 '25 15:08 mbrandonw