Skip to content

Inline Snapshot Testing#199

Merged
mbrandonw merged 7 commits into
pointfreeco:masterfrom
rjchatfield:inline-snapshot-testing
Mar 29, 2019
Merged

Inline Snapshot Testing#199
mbrandonw merged 7 commits into
pointfreeco:masterfrom
rjchatfield:inline-snapshot-testing

Conversation

@rjchatfield

Copy link
Copy Markdown
Contributor

This PR aims to add a new string literal-based API:

assertInlineSnapshot(of: user, as: .dump, toMatch: """
▿ User
  - bio: "Blobbed around the world."
  - id: 1
  - name: "Blobby"
""")

To record, set record = true or provide an empty string:

assertInlineSnapshot(of: user, as: .dump, toMatch: """
""")
// or
assertInlineSnapshot(of: user, as: .dump, toMatch: "")

Recording magic in action:

slide_for_effect

I've done a talk and written a blog post on the idea. And I have plenty more to share. ;)

Happy for feedback.

@stephencelis

Copy link
Copy Markdown
Member

This is awesome! The reason the build is failing is because we currently have to share logic with a SnapshotTestCase class on Linux, so you'll need to duplicate things there. Hopefully with Swift 5 we will be able to stop worrying about this, though. Assuming we drop Swift 4 compatibility today we can rebase this branch and hopefully everything just passes!

@stephencelis

Copy link
Copy Markdown
Member

Alright, 1.4.0 is out and I merged it into this branch! I think @mbrandonw and I may tweak the API and some of the messaging to make it consistent with other errors, but assuming it goes green I think we'll merge first!

@stephencelis

Copy link
Copy Markdown
Member

Just wanted to let ya know we plan on taking a look at this more in depth tomorrow and will merge if all looks good!

@mbrandonw

Copy link
Copy Markdown
Member

Hey @rjchatfield! Stephen and I just discussed this IRL and we really like what you've got! We made a few small changes to the API names here ffc714c, and then we moved the inline assert helpers to their own file here 8f62af6.

We also decided to underscore the inline assert helpers so that we can use them a bit in our production code bases before we make it fully public. The helpers are marked as public, but this way since they are underscored we can iterate a bit on the API in case we find some rough edges.

We're going to merge now, and we'll let you know of any changes we decide to make in the future, and hopefully we'll get the non-underscored versions in our 1.5 release!

Thanks again for this, it's really awesome and we had never even considered this type of snapshot testing before!

@mbrandonw mbrandonw merged commit ab119d1 into pointfreeco:master Mar 29, 2019
@rjchatfield

Copy link
Copy Markdown
Contributor Author

Brilliant. Thanks guys. I got busier as the week went on, so I didn’t get a chance to come back to this.

One of the big things I wanted to highlight was how record works. I was always performing the diff under the hood, and if you pass then I always bail out there. So even if you’re recording, I wanted to avoid touching the source files and showing errors in your test when nothing had changed. This was definitely important when we rerun all tests in our codebase to see if anything changed.

But it seems you merged it in. So that’s great. I have some other ideas for improvements, but I can make separate PRs.

Thanks again for being open to the idea. Your library has really helped us, so I hope this helps you.

isismsilva referenced this pull request in powerhome/playbook-swift Oct 2, 2023
#146)

[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Update | Change |
|---|---|---|
|
[pointfreeco/swift-snapshot-testing](https://togithub.com/pointfreeco/swift-snapshot-testing)
| minor | `from: "1.11.1"` -> `from: "1.13.0"` |

---

### Release Notes

<details>
<summary>pointfreeco/swift-snapshot-testing
(pointfreeco/swift-snapshot-testing)</summary>

###
[`v1.13.0`](https://togithub.com/pointfreeco/swift-snapshot-testing/releases/tag/1.13.0)

[Compare
Source](https://togithub.com/pointfreeco/swift-snapshot-testing/compare/1.12.0...1.13.0)

#### What's Changed

- Added: *Inline* Snapshot Testing
([https://github.com/pointfreeco/swift-snapshot-testing/pull/764](https://togithub.com/pointfreeco/swift-snapshot-testing/pull/764)).
This allows your text-based snapshots to live right in the test source
code, rather than in an external file:


![inline-snapshot](https://togithub.com/pointfreeco/swift-snapshot-testing/assets/658/832172a0-ec62-42b8-aba8-79ac9143df08)

While the library has had experimental support for this feature since
[1.5.0](https://togithub.com/pointfreeco/swift-snapshot-testing/releases/1.5.0)
thanks to [@&#8203;rjchatfield](https://togithub.com/rjchatfield)
([https://github.com/pointfreeco/swift-snapshot-testing/pull/199](https://togithub.com/pointfreeco/swift-snapshot-testing/pull/199)),
we've finally put the finishing touches to it:

- Inline snapshot testing is available in a separate
`InlineSnapshotTesting` module. To use inline snapshot testing, add a
dependency on this module and update your existing imports:

        ```diff
        -import SnapshotTesting
        +import InlineSnapshotTesting
        ```

The feature has been rewritten to use
[SwiftSyntax](https://togithub.com/apple/swift-syntax). While a
heavyweight dependency, it is a more reasonable tool for generating
Swift code than string substitution, and will be an increasingly common
dependency as the de facto tool for writing Swift macros.

The main `SnapshotTesting` module does not depend on SwiftSyntax, so
existing snapshot tests will not incur cost of compiling SwiftSyntax.

- 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:

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

After this assertion runs, the test source code is updated in place:

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

These trailing closures are easy to select in Xcode in order to delete
and re-record a snapshot: simply double-click one of the braces to
highlight the closure, delete, and run the test.

- Inline snapshotting's `assertInlineSnapshot` testing tool is fully
customizable so that you can build your own testing helpers on top of it
without your users even knowing they are using snapshot testing. In
fact, we do this to create a testing tool that helps us test the Swift
code that powers [Point-Free](https://www.pointfree.co). It's called
[`assertRequest`][assert-request-gh], and it allows you to
simultaneously assert the request being made to the server (including
URL, query parameters, headers, POST body) as well as the response from
the server (including status code and headers).

For example, to test that when a request is made for a user to join a
team subscription, we can [write the following][assert-request-example]:

        ```swift
        await assertRequest(
          connection(
            from: request(
to: .teamInviteCode(.join(code: subscription.teamInviteCode, email:
nil)),
              session: .loggedIn(as: currentUser)
            )
          )
        )
        ```

And when we first run the test it will automatically
[expand][assert-request-example]:

        ```swift
        await assertRequest(
          connection(
            from: request(
to: .teamInviteCode(.join(code: subscription.teamInviteCode, email:
nil)),
              session: .loggedIn(as: currentUser)
            )
          )
        ) {
          """
POST http://localhost:8080/join/subscriptions-team_invite_code3
Cookie: pf_session={"userId":"00000000-0000-0000-0000-000000000001"}
          """
        } response: {
          """
          302 Found
          Location: /account
          Referrer-Policy: strict-origin-when-cross-origin
Set-Cookie: pf_session={"flash":{"message":"You now have access to
Point-Free!","priority":"notice"},"userId":"00000000-0000-0000-0000-000000000001"};
Expires=Sat, 29 Jan 2028 00:00:00 GMT; Path=/
          X-Content-Type-Options: nosniff
          X-Download-Options: noopen
          X-Frame-Options: SAMEORIGIN
          X-Permitted-Cross-Domain-Policies: none
          X-XSS-Protection: 1; mode=block
          """
        }
        ```

This shows that the response redirects the use back to their account
page and shows them the flash message that they now have full access to
Point-Free. This makes writing complex and nuanced tests incredibly
easy, and so there is no reason to not write lots of tests for all the
subtle edge cases of your application's logic.

- Added: DocC documentation
([#&#8203;765](https://togithub.com/pointfreeco/swift-snapshot-testing/issues/765)).
The `SnapshotTesting` and `InlineSnapshotTesting` are fully documented
using DocC.

- Infrastructure: swift-format support
([#&#8203;765](https://togithub.com/pointfreeco/swift-snapshot-testing/issues/765)).
The library is now auto-formatted using swift-format.

**Full Changelog**:
pointfreeco/swift-snapshot-testing@1.12.0...0.13.0

[assert-request-gh]:
https://togithub.com/pointfreeco/pointfreeco/blob/5b5cd26d8240bd0e1afb77b7ef342458592c7366/Sources/PointFreeTestSupport/PointFreeTestSupport.swift#L42-L87

[assert-request-example]:
https://togithub.com/pointfreeco/pointfreeco/blob/a237ce693258b363ebfb4bdffe6025cc28ac891f/Tests/PointFreeTests/JoinMiddlewareTests.swift#L285-L309

###
[`v1.12.0`](https://togithub.com/pointfreeco/swift-snapshot-testing/releases/tag/1.12.0)

[Compare
Source](https://togithub.com/pointfreeco/swift-snapshot-testing/compare/1.11.1...1.12.0)

#### What's Changed

- Added: `assertSnapshot(of:)` is now the default interface for snapshot
assertions
([https://github.com/pointfreeco/swift-snapshot-testing/pull/762](https://togithub.com/pointfreeco/swift-snapshot-testing/pull/762)).
`assertSnapshot(matching:)` will remain in 1.x as a soft-deprecated
alias.
- Infrastructure: Add to README plug-ins (thanks
[@&#8203;BarredEwe](https://togithub.com/BarredEwe),
[https://github.com/pointfreeco/swift-snapshot-testing/pull/746](https://togithub.com/pointfreeco/swift-snapshot-testing/pull/746);
[@&#8203;tahirmt](https://togithub.com/tahirmt),
[https://github.com/pointfreeco/swift-snapshot-testing/pull/763](https://togithub.com/pointfreeco/swift-snapshot-testing/pull/763)).
- Infrastructure: Don't ignore Package.resolved
([https://github.com/pointfreeco/swift-snapshot-testing/pull/649](https://togithub.com/pointfreeco/swift-snapshot-testing/pull/649)).

#### New Contributors

- [@&#8203;BarredEwe](https://togithub.com/BarredEwe) made their first
contribution in
[https://github.com/pointfreeco/swift-snapshot-testing/pull/746](https://togithub.com/pointfreeco/swift-snapshot-testing/pull/746)

**Full Changelog**:
pointfreeco/swift-snapshot-testing@1.11.1...1.12.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/powerhome/PlaybookSwift).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNi42OC4xIiwidXBkYXRlZEluVmVyIjoiMzYuODMuMCIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants