Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis committed Jul 4, 2024
1 parent aaf6ce4 commit f98a7e7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ These customization options have a few downsides currently.

And the ``diffTool`` variable allows one to specify a command line tool to use for visualizing
diffs of files, but only works when the command line tool accepts a very narrow set of arguments,
_e.g._ `ksdiff /path/to/file1.png /path/to/file2.png`.
_e.g._:

```sh
$ ksdiff /path/to/file1.png /path/to/file2.png
```

We have greatly improved upon all of these problems by introducing the new
``withSnapshotTesting(record:diffTool:operation:)-2kuyr`` tool for customizing snapshots. It
Expand All @@ -51,7 +55,7 @@ Rather than overriding `isRecording` or `diffTool` directly in your tests, you c
func testFeature() {
isRecording = true
diffTool = "ksdiff"
assertSnapshot()
assertSnapshot(/* ... */)
}
```
}
Expand All @@ -60,8 +64,11 @@ Rather than overriding `isRecording` or `diffTool` directly in your tests, you c
// After

func testFeature() {
withSnapshotTesting(record: .all, diffTool: .ksdiff) {
assertSnapshot(…)
withSnapshotTesting(
record: .all,
diffTool: .ksdiff
) {
assertSnapshot(/* ... */)
}
}
```
Expand Down Expand Up @@ -95,7 +102,10 @@ method of `XCTestCase`:

class FeatureTests: XCTestCase {
override func invokeTest() {
withSnapshotTesting(diffTool: .ksdiff, record: .all) {
withSnapshotTesting(
record: .all,
diffTool: .ksdiff
) {
super.invokeTest()
}
}
Expand Down Expand Up @@ -124,15 +134,15 @@ Further, the `diffTool` and `record` arguments have extra customization capabili

* `record` is now a [type](<doc:SnapshotTestingConfiguration/Record-swift.struct>) with 4
choices: `all`, `missing`, `never`, `failed`:
* `all`: All snapshots will be generated and saved to disk.
* `missing`: only the snapshots that are missing from the disk will be generated
and saved.
* `never`: No snapshots will be generated, even if they are missing. This option is appropriate
when running tests on CI so that re-tries of tests do not surprisingly pass after snapshots are
unexpectedly generated.
* `failed`: Snapshots only for failing tests will be generated. This can be useful for tests
that use precision thresholds so that passing tests do not re-record snapshots that are
subtly different but still within the threshold.
* `all`: All snapshots will be generated and saved to disk.
* `missing`: only the snapshots that are missing from the disk will be generated
and saved.
* `never`: No snapshots will be generated, even if they are missing. This option is
appropriate when running tests on CI so that re-tries of tests do not surprisingly pass
after snapshots are unexpectedly generated.
* `failed`: Snapshots only for failing tests will be generated. This can be useful for tests
that use precision thresholds so that passing tests do not re-record snapshots that are
subtly different but still within the threshold.

## Beta support for Swift Testing

Expand All @@ -157,7 +167,7 @@ you to customize snapshots for a `@Test` or `@Suite`, but to get access to it yo
```swift
@_spi(Experimental) import SnapshotTesting

@Suite(.snapshots(diffTool: .ksdiff, record: .all))
@Suite(.snapshots(record: .all, diffTool: .ksdiff))
struct FeatureTests {
}
Expand Down
15 changes: 8 additions & 7 deletions Sources/SnapshotTesting/SnapshotTestingConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ public func withSnapshotTesting<R>(
) rethrows -> R {
try SnapshotTestingConfiguration.$current.withValue(
SnapshotTestingConfiguration(
diffTool: diffTool ?? SnapshotTestingConfiguration.current?.diffTool ?? SnapshotTesting._diffTool,
record: record ?? SnapshotTestingConfiguration.current?.record ?? _record
record: record ?? SnapshotTestingConfiguration.current?.record ?? _record,
diffTool: diffTool ?? SnapshotTestingConfiguration.current?.diffTool
?? SnapshotTesting._diffTool
)
) {
try operation()
Expand All @@ -48,8 +49,8 @@ public func withSnapshotTesting<R>(
) async rethrows -> R {
try await SnapshotTestingConfiguration.$current.withValue(
SnapshotTestingConfiguration(
diffTool: diffTool ?? SnapshotTestingConfiguration.current?.diffTool ?? _diffTool,
record: record ?? SnapshotTestingConfiguration.current?.record ?? _record
record: record ?? SnapshotTestingConfiguration.current?.record ?? _record,
diffTool: diffTool ?? SnapshotTestingConfiguration.current?.diffTool ?? _diffTool
)
) {
try await operation()
Expand All @@ -72,11 +73,11 @@ public struct SnapshotTestingConfiguration: Sendable {
public var record: Record

public init(
diffTool: DiffTool = .default,
record: Record = .missing
record: Record = .missing,
diffTool: DiffTool = .default
) {
self.diffTool = diffTool
self.record = record
self.diffTool = diffTool
}

/// The record mode of the snapshot test.
Expand Down
12 changes: 7 additions & 5 deletions Sources/SnapshotTesting/SnapshotsTestTrait.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@
@_spi(Experimental)
extension Trait where Self == _SnapshotsTestTrait {
/// Configure snapshot testing in a suite or test.
///
/// - Parameters:
/// - diffTool: The diff tool to use in failure messages.
/// - record: The record mode of the test.
/// - diffTool: The diff tool to use in failure messages.
public static func snapshots(
diffTool: SnapshotTestingConfiguration.DiffTool = .default,
record: SnapshotTestingConfiguration.Record = .missing
record: SnapshotTestingConfiguration.Record = .missing,
diffTool: SnapshotTestingConfiguration.DiffTool = .default
) -> Self {
_SnapshotsTestTrait(
configuration: SnapshotTestingConfiguration(
diffTool: diffTool,
record: record
record: record,
diffTool: diffTool
)
)
}

/// Configure snapshot testing in a suite or test.
///
/// - Parameter configuration: The configuration to use.
public static func snapshots(
_ configuration: SnapshotTestingConfiguration
Expand Down

0 comments on commit f98a7e7

Please sign in to comment.