pointfreeco / swift-snapshot-testing

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

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

Closed JustasL closed 1 year ago

JustasL commented 1 year ago

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

markst commented 1 year ago

@JustasL there is also loadViewIfNeeded()

markst commented 1 year ago

Might also be related to #717

JustasL commented 1 year ago

@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