pointfreeco / swift-snapshot-testing

📸 Delightful Swift snapshot testing.
https://www.pointfree.co/episodes/ep41-a-tour-of-snapshot-testing
MIT License
3.73k stars 565 forks source link

Formalize Inline Snapshot Testing #764

Closed stephencelis closed 11 months ago

stephencelis commented 11 months ago

Inline snapshot testing was introduced by @rjchatfield in #199 as an underscored interface. It's a fantastic tool that we've used a lot, but we never got around to finalizing it with a more public interface. This PR does just that, and evolves the original helper in a few ways:

  1. The API now follows the same structure as assertSnapshot, except it uses a trailing closure to capture the inline snapshot. This makes it easy to update an existing snapshot test to use inline snapshots:

    -assertSnapshot(of: user, as: .json)
    +assertInlineSnapshot(of: user, as .json)

    And after recording, it looks something like this:

    assertInlineSnapshot(of: user, as: .json) {
      """
      {
        "name" : "Blob",
        "isAdmin" : true
      }
      """
    }

    Trailing closures are easy to select in Xcode for deletion and re-recording.

  2. Inline snapshots now use SwiftSyntax for inlining snapshots into Swift code. While a heavyweight dependency, it is a more reasonable tool for writing Swift code than string substitution.

  3. Inline snapshotting is available in a separate InlineSnapshotTesting module. This is so existing snapshot tests do not need to incur the SwiftSyntax dependency.

  4. Inline snapshotting is now extensible. It is possible to write your own inline snapshot helpers.