swift-snapshot-testing icon indicating copy to clipboard operation
swift-snapshot-testing copied to clipboard

Fix crash when using `UIWindow` in `assertSnapshot`

Open zenangst opened this issue 5 years ago • 11 comments

prepareView in View.swift will now check if the current snapshot target is a UIWindow. If that condition is true, then it will return early and use the window as its dispose value. It will make the assumption that the window has a rootViewController set and use that as it's viewController.

This avoids unbalanced calls to appearance transitions during test teardown which will throw exceptions and crash the test.

zenangst avatar Jul 02 '20 12:07 zenangst

@stephencelis mind taking a look a this? 😎

zenangst avatar Aug 11 '20 13:08 zenangst

Hey @zenangst! Thanks for taking the time to submit this and sorry if things are taking a bit longer to take a look at.

Given the early out does this mean that the traits and sizing will not be applied to the window?

stephencelis avatar Aug 11 '20 14:08 stephencelis

Yeah traits and sizing should be left out. Maybe that is a false assumption on my part but if you give it the window then everything down the line should already be handled in your test. What do you think?

zenangst avatar Aug 11 '20 14:08 zenangst

@stephencelis I pushed another commit to include the traits if the root view controller of the window could be resolved. Is that an okay assignment seeing that rootViewController is optional in UIWindow and it is non-optional on the add method. Or should we fail early here and just throw an exception if the root view controller cannot be resolved?

zenangst avatar Aug 11 '20 14:08 zenangst

ping @stephencelis 😎

zenangst avatar Aug 18 '20 08:08 zenangst

Any headway here? Something that needs changing before it can be merged? 🌞

zenangst avatar Aug 24 '20 08:08 zenangst

@stephencelis

i3K1nMkuEtBeR

zenangst avatar Oct 27 '20 11:10 zenangst

Please merge, I also encounter this issue

dmitrysimkin avatar May 21 '21 17:05 dmitrysimkin

I merged this with main now 😎

zenangst avatar Oct 25 '21 09:10 zenangst

@stephencelis will you merge this one, we are also having the same issue and it has been more then one year since @zenangst requested this PR?

That would be great if you merge, we don't want to continue with forked one, thanks :)

okankocyigit avatar Nov 11 '21 14:11 okankocyigit

We have those changes on our fork for a while now and it works perfectly. Maybe this could be somehow merged? 🐱🙏 @stephencelis ? :)

krzysztofpawski avatar Apr 01 '22 09:04 krzysztofpawski