Introducing SnapshotTestingMacros
I've been working on a swift macro library to generate snapshot tests in a format and structure similar to Swift Testing.
Instead of @Suite
and @Test
you use @SnapshotSuite
and @SnapshotTest
.
View more (including documentation) here: GitHub - adammcarter/swift-snapshot-testing-macros: A Swift Macro library for generating snapshot tests from functions
Create snapshots like this
@SnapshotSuite
struct MySnapshots {
@SnapshotTest
func myView() -> some View {
Text("Some text")
}
}
Traits
Specify device sizes, theme (dark/light mode), background colours, padding and even force record and set snapshot strategies.
@SnapshotSuite
struct MySnapshots {
@SnapshotTest(
.sizes(devices: .iPhoneX, .iPadPro11),
.backgroundColor(.red),
.padding(20),
.record
)
func myPhoneView() -> some View {
Text("Some text here")
}
}
Traits can also be set on the suite so all tests inherit them.
And sensible defaults exist, such as automatically creating light and dark mode snapshot variants.
Parameterisation
configurations
@Suite
@SnapshotSuite
struct MySnapshots {
@SnapshotTest(
configurations: [ // ⬅️ Set configurations to pass names and values
SnapshotConfiguration(name: "Name 1", value: 1),
SnapshotConfiguration(name: "Name 2", value: 2),
]
)
func myView(value: Int) -> some View {
Text("value: \(value)")
}
}
Folder structure:
configurationValues
@Suite
@SnapshotSuite
struct MySnapshots {
@SnapshotTest(configurationValues: [1, 2]) // ⬅️ Set configurationValues to pass values and infer the name from the value
func myView(int: Int) -> some View {
Text("value: \(int)")
}
}
Folder structure:
And more
See the documentation on the README of the repo for a more comprehensive list of things you can do and example code.
Feedback
It's still in development but it would be great to get some early feedback from people who are interested in using this and fixing up any issues, adding new features or just discussing any technical sides of this macro.
Thanks!