Skip to content

Instantly share code, notes, and snippets.

@ethanhuang13
Created August 21, 2023 01:24
Show Gist options
  • Save ethanhuang13/af28cb18cbffc7a785e9eb5869d6902d to your computer and use it in GitHub Desktop.
Save ethanhuang13/af28cb18cbffc7a785e9eb5869d6902d to your computer and use it in GitHub Desktop.
// iOS 17's JSONEncoder is rarely producing a different key sorting order(< 5%), which is ok for JSON itself, but will cause flaky tests.
// It wasn't happened pre-iOS 17.
// https://mastodon.social/@ethanhuang13/110904915670262949
import XCTest
final class JSONEncoderTests: XCTestCase {
func testEncode() {
struct Model: Codable {
var a: String = ""
var b: String = ""
var c: String = ""
}
let model = Model()
let encoder = JSONEncoder()
// encoder.outputFormatting = .sortedKeys // Uncomment to fix the flaky test
let data1 = try! encoder.encode(model)
let data2 = try! encoder.encode(model)
XCTAssertEqual(
String(decoding: data1, as: UTF8.self),
String(decoding: data2, as: UTF8.self)
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment