Swiftpack.co - whiteio/SnapGen as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by whiteio.
whiteio/SnapGen 0.4.1
📸 Automatically generate snapshot tests for SwiftUI previews
⭐️ 0
🕓 1 week ago
macOS
.package(url: "https://github.com/whiteio/SnapGen.git", from: "0.4.1")

SnapGen

Automatically generate snapshot tests for SwiftUI previews

Github Actions

Installation

Swift Package Manager

Add SnapGen and SnapshotTesting as dependencies in Package.swift.

dependencies: [
  .package(
    url: "https://github.com/whiteio/SnapGen",
    revision: "5c21f026eebb8cc8658c1d1fb5f3931909a2871a"
  ),
]

Add SnapGenTesting as a dependency of your test target

targets: [
  .target(name: "MyApp"),
  .testTarget(
    name: "MyAppTests",
    dependencies: [
      "MyApp",
      .product(name: "SnapGenTesting", package: "SnapGen"),
    ]
  )
]

Using the command line tool

Clone the repo

git clone https://github.com/whiteio/SnapGen

Build and run through the command line

swift build
swift run

Usage

Command Line

swift run

Here's the optional command line arguments

--input Directory or file containing SwiftUI previews to generate snapshots for
--output Directory to store the generate preview snapshots
--module Name of the module containing the previews (used as @testable import ___MODULENAME___)

Example:

swift run --input Sources/ --output Tests/ --module ModuleName

YAML configuration

Alternatively to passing command line arguments, you can add a YAML configuration file

Create a file called .snapgen.yml in the project directory

touch .snap-gen.yml

Add the following structure

excluded:
  - /Users/whiteio/Development/SnapGen/Sources/SnapGenTesting
input: /Users/whiteio/Development/SnapGen/Sources/SnapGen/Sources
output: /Users/whiteio/Development/SnapGen/Sources/SnapGen/Tests
module: SnapGen

This will do the following:

  • Avoid generating previews for the SnapGenTesting target
  • Set the input path
  • Set the output path
  • Set the module name which will be imported from the generated test files

YAML configurations take precendence over command line arguments.

Excluding SwiftUI Previews

SwiftUI previews can be excluded by adding an annotation above the struct which conforms to PreviewProvider to prevent snapshot tests being generated for it. For example:

// snapgen skip
struct ExampleView_Previews: PreviewProvider {
  static var previews: some View {
    ExampleView()
  }
}

Annotations can be interleaved with documentation comments.

Troubleshooting

Due to the way images are created from SwiftUI previews the following error snapshot may be produced:

Screen Shot 2023-03-15 at 21 31 13

To resolve this, the preview can be set to a fixed size, e.g.

struct ExampleView_Previews: PreviewProvider {
  static var previews: some View {
    ExampleView()
      .previewLayout(.fixed(width: 400, height: 900))
  }
}

GitHub

link
Stars: 0
Last commit: 1 week ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics