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

Quick proposal to fix .wait method #742

Open JustasL opened 1 year ago

JustasL commented 1 year ago

As mentioned in this issue Snapshotting.wait does not work UIViewController if view property is not accessed

From quick debugging it seems it's because view is created, then snapshotting waits for delay period, and only then it setups views and quickly disposes them

My proposed work-a-round is to actually delays test assertion from being ran

markst commented 1 year ago

@JustasL makes more sense to extend Async:

extension Async {
  func delay(by timeInterval: TimeInterval) -> Async<Value> {
    return Async<Value> { callback in
      self.run { value in
        DispatchQueue.main.asyncAfter(deadline: .now() + timeInterval) {
          callback(value)
        }
      }
    }
  }
}

then you can chain:

addImagesForRenderedViews(view).sequence().delay(by: 1.0).run { views in
markst commented 1 year ago

@JustasL can you make amends to bring in these changes? https://github.com/markst/swift-snapshot-testing/commit/029c66aa6ad8cf226025576dbd0289cfea5e800d

Also make it clear to mention that this delay needs to occur between presentation to window prepareView & renderer. https://github.com/pointfreeco/swift-snapshot-testing/issues/717#issuecomment-1603426150

JustasL commented 1 year ago

@markst updated PR, although was not sure where to put comment about:

delay needs to occur between presentation to window prepareView & renderer.

kamvoick commented 1 year ago

SwiftUI View does not work also with wait strategy, and requires this workaround here https://github.com/pointfreeco/swift-snapshot-testing/blob/dc46eeb3928a75390651fac6c1ef7f93ad59a73b/Sources/SnapshotTesting/Snapshotting/SwiftUIView.swift#L74

And i confirm that adding this delay solves it with SwiftUI

lihao6485 commented 1 year ago

Thanks @JustasL, the new .delay function solved my issue when a view required to load mock network data on viewWillAppear. We always getting inconsistence result, for example loading indicator is not dismiss when using .wait function.

@stephencelis possible to take a look this PR? Thanks.

r3econ commented 12 months ago

Is there any timeline for fixing it? The problem affects our test workflows

WJacobsNL commented 11 months ago

We are currently putting our tests to Sleep in order to wait for loading to finish, would be helpful if this fix would get released :)

jmorgaz-freenow commented 7 months ago

Thanks @JustasL for this workaround. I can confirm that works in our SwiftUI views too. We've created a fork including your changes but could be nice if those could be added in a regular release, any news about that?

taquitos commented 7 months ago

@stephencelis, would you mind taking a peek at this? I'd hate to have to fork the project just to pull this 🙃

AF-cgi commented 7 months ago

Any updates here?

@JustasL Could you update your PR to the latest api of this package? I ask you because there are some notice of deprecated api's like asseertShnapshot(matching: ...).

@markst Could you release a new version including of this fix?

mbaikovets commented 5 months ago

Hello! @stephencelis , what should be done to move this proposal forward and introduce new version of package with this changes? If there anything we as community can help with - please, let us know.

Thank you in advance!

jmorgaz-freenow commented 3 months ago

Hi! It's been a while since this MR is opened and maybe there is someone else besides @stephencelis that could help us with it?

AndreiMatrix commented 3 months ago

@stephencelis is it posible to merge this fix?

TomVanDerSpek commented 2 months ago

Also waiting for a fix on this, what is the status?