Skip to content

Added Snapshotting.json for Any value #552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 7, 2022

Conversation

NachoSoto
Copy link
Contributor

@NachoSoto NachoSoto commented Dec 21, 2021

Currently the only way to snapshot Any value is using dump. However, the format isn't always as readable as it could be.
For data that can be encoded in JSON, this new overload provides a much more readable output.

Currently the only way to snapshot `Any` value is using `dump`. However, the format isn't always as readable as it can be.
For data that can be encoded in JSON, this new overload provides a much more readable output.
@mbrandonw
Copy link
Member

The library ships with a .json strategy that works for Encodable types:

https://github.com/pointfreeco/swift-snapshot-testing/blob/main/Sources/SnapshotTesting/Snapshotting/Codable.swift

And we have some tests to show how it works:

func testEncodable() {
struct User: Encodable { let id: Int, name: String, bio: String }
let user = User(id: 1, name: "Blobby", bio: "Blobbed around the world.")
if #available(iOS 11.0, macOS 10.13, tvOS 11.0, *) {
assertSnapshot(matching: user, as: .json)
}
assertSnapshot(matching: user, as: .plist)
}

Does this cover your use case?

@NachoSoto
Copy link
Contributor Author

In the test that I added I'm using an Encodable type for convenience, but not all types are Encodable.

The specific use case I'm covering here is this: I have a type from an old Swift implementation that does "manual" JSON encoding into a [String: Any]. I'm working on converting that to use Codable, but before I do I wanted to add a snapshot test for that [String: Any] to ensure that the new Codable implementation doesn't change the output.

@mbrandonw
Copy link
Member

Ah yes, I see now. Two things then: 1.) I think the strategy needs to be public, and 2.) could you update the test to use something that is not encodable, such as a [String: Any] dictionary?

`.sortedKeys` is only available from `macOS 10.13`.
@NachoSoto
Copy link
Contributor Author

Oh yes my bad, changed to public.

As for the test, it's already using Any (the output of JSONSerialization.jsonObject). That's a [String: Any], but I can cast it if you prefer.

@mbrandonw
Copy link
Member

Ah ok, I breezed past that. We can leave it as-is.

@NachoSoto
Copy link
Contributor Author

Anything else I can do? :)

NachoSoto added a commit to RevenueCat/purchases-ios that referenced this pull request Dec 22, 2021
For #1045.
Snapshot testing is a more maintainable and readable way to ensure that the output format doesn't change.
In this case, I'm using it to test `ProductInfo.asDictionary()`, as a way to ensure that #1107 doesn't introduce changes.
We can use this to check other types in the future, like `SubscriberAttribute`, `ETagAndResponseWrapper`, etc.


This uses [`swift-snapshot-testing`](https://github.com/pointfreeco/swift-snapshot-testing).
The simple line `try assertSnapshot(matching: productInfo.asDictionary(), as: .json)` automatically checks against a stored file (`PurchasesTests/Purchasing/__Snapshots__/ProductInfoTests/testEncoding.1.json`). If the output changes, the test error is very readable:
<img width="228" alt="Screen Shot 2021-12-21 at 15 27 57" src="https://user-images.githubusercontent.com/685609/147010663-88eddc69-b106-46f5-a8b5-0ee927882282.png">

I've added the snapshot folder as a reference folder in Xcode, which makes looking at the expected output much easier than a bunch of Swift lines checking each value in a dictionary:
<img width="178" alt="Screen Shot 2021-12-21 at 15 26 31" src="https://user-images.githubusercontent.com/685609/147010570-dd68f1ec-b0b0-4570-b59f-8451dcaf05e2.png">

I've also submitted a PR (pointfreeco/swift-snapshot-testing#552) based on the small extension added here.
@mbrandonw
Copy link
Member

Sorry for the delay. Thanks for this!

@mbrandonw mbrandonw merged commit a443288 into pointfreeco:main Jan 7, 2022
@stephencelis stephencelis mentioned this pull request Jan 7, 2022
@NachoSoto NachoSoto deleted the json-encoding-dictionary branch March 4, 2022 16:19
@NachoSoto
Copy link
Contributor Author

Any chance we can get a release including this?

niil-qb pushed a commit to quickbit/swift-snapshot-testing that referenced this pull request Feb 23, 2023
* Added `Snapshotting.json` for `Any` value

Currently the only way to snapshot `Any` value is using `dump`. However, the format isn't always as readable as it can be.
For data that can be encoded in JSON, this new overload provides a much more readable output.

* Fixed macOS build

`.sortedKeys` is only available from `macOS 10.13`.

* Public extension
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.

2 participants