pointfreeco / swift-snapshot-testing

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

UIScreen bounds are not correct in Snapshop testing #719

Open FkMarcus opened 1 year ago

FkMarcus commented 1 year ago

I'm trying to use this framework for snapshot testing from some view controllers. It works fine in most cases, but I have one big issue. for example here is my test

func test_onboarding_iPhone13() {
    let vc = OnboardingViewController()
    assertSnapshot(matching: vc, as: .image(on: .iPhone13(.portrait)), record: true)
}

So, I'm trying to run a test on an iPhone 13. In the view controller, I use UIScreen.main.bounds to calculate some sizes, and it works fine when the test is not running. However, in the snapshot test, the value of UIScreen.main.bounds is different, causing issues.

Here is UIScreen.main.bounds when I run app on simulation (not testing)

(lldb) p UIScreen.main.bounds
(CGRect) $R0 = (origin = (x = 0, y = 0), size = (width = 390, height = 844))

but in UI testing

(lldb) p UIScreen.main.bounds
(CGRect) $R0 = (origin = (x = 0, y = 0), size = (width = 320, height = 480))

Am I doing something wrong here? Or it is a bug? Thanks you for your response in advance

AndreyAnt commented 1 year ago

Same thing happens for me, I've got approx 1k test cases in the project, and some of them (very few 10-20) fails. I've tried several different approaches: comparing UIViewControllers with ViewImageConfig, comparing UIViews with custom size set and even writing custom snapshotting strategy. All of them somehow get down to the minimal size (width = 320, height = 480) I finished with commenting out the failing tests 😔