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

Snapshotting.wait on UIViewController does not work if view is not referenced

JustasL opened this issue · comments

Describe the bug

Snapshotting.wait does not work unless view value is read
I've created a demo repo, that's very simple, contains framework, snapshot test target

To Reproduce

Demo repo: https://github.com/JustasL/DelayedSnapshotingDemo

Run testDelayedSnapshoting in my DemoUIKitTests
The generated UIViewController snapshot screenshot will have a red background

class DemoViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .red
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
            self.view.backgroundColor = .green
        }
    }
}
final class DemoUIKitTests: XCTestCase {

    func testDelayedSnapshoting() throws {
        let vc = DemoViewController()
        
        assertSnapshot(
            matching: vc,
            as: .wait(for: 2, on: .image(on: .iPhone13))
        )
    }
}

Expected behavior

While I would expect to see a green background, due to delay in viewDidLoad and wait

One work-a-round for this issue is to call

let _ = vc.view

Before calling assertSnapshot, but then if we have UI setup logic in viewWillAppear with delay, the assertSnapshot method breaks again

Environment

  • swift-snapshot-testing version 1.11.0
  • Xcode 14.3

@JustasL there is also loadViewIfNeeded()

Might also be related to #717

@JustasL there is also loadViewIfNeeded()

Yes, but it does not call viewWillAppear/viewDidAppear

Might also be related to #717

Closing issue because it is related to #717