pointfreeco / swift-snapshot-testing

📸 Delightful Swift snapshot testing.

Home Page:https://www.pointfree.co/episodes/ep41-a-tour-of-snapshot-testing

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

UIScreen bounds are not correct in Snapshop testing

FkMarcus opened this issue · comments

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

  • Environment

    swift-snapshot-testing version 1.10.0
    Xcode 14.2
    Swift 5.7.2
    OS: 16.2

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 😔