Automatically generate snapshot tests for SwiftUI previews
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"),
]
)
]
Clone the repo
git clone https://github.com/whiteio/SnapGen
Build and run through the command line
swift build
swift run
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
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:
SnapGenTesting
targetYAML configurations take precendence over command line arguments.
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.
Due to the way images are created from SwiftUI previews the following error snapshot may be produced:
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))
}
}
link |
Stars: 0 |
Last commit: 1 week ago |
Full Changelog: https://github.com/whiteio/SnapshotGen/compare/0.2.2...0.3.0
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics