Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when using UIWindow in assertSnapshot #366

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

zenangst
Copy link

@zenangst zenangst commented Jul 2, 2020

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.

`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.
Rely on configuration options instead.
@zenangst zenangst force-pushed the fix/crash-when-using-uiwindow-in-assertSnapshot branch from 39c79e2 to e04b8e1 Compare August 10, 2020 09:56
@zenangst
Copy link
Author

@stephencelis mind taking a look a this? 😎

@stephencelis
Copy link
Member

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?

@zenangst
Copy link
Author

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
Copy link
Author

@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?

if let rootViewController = window.rootViewController {
return add(traits: traits, viewController: rootViewController, to: window)
} else {
return {}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add an assertFailure here

@zenangst
Copy link
Author

ping @stephencelis 😎

@zenangst
Copy link
Author

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

schalkwijk pushed a commit to schalkwijk/swift-snapshot-testing that referenced this pull request Sep 6, 2020
This way, we don't accidentally hit bad transition points in tests, as per pointfreeco#366. Basically, because we're necessarily running our tests inside of a hosted application, the snapshot library incorrectly creates yet another window in which to run tests, which confuses the ... iOS runtime? or something and causes the app to queue up a bunch of appear / disappear transitions that clog up the main thread until they just halt the entire app, causing the app to stop for a solid 20-30 seconds, which explains timeout issues we're having in CI and occasionally locally.

The cost of this is that the `onAppear` triggers no longer trigger in snapshot tests, but that's a small price to pay to make sure the tests actually pass in CI.
schalkwijk pushed a commit to schalkwijk/swift-snapshot-testing that referenced this pull request Sep 7, 2020
This way, we don't accidentally hit bad transition points in tests, as per pointfreeco#366. Basically, because we're necessarily running our tests inside of a hosted application, the snapshot library incorrectly creates yet another window in which to run tests, which confuses the ... iOS runtime? or something and causes the app to queue up a bunch of appear / disappear transitions that clog up the main thread until they just halt the entire app, causing the app to stop for a solid 20-30 seconds, which explains timeout issues we're having in CI and occasionally locally.

The cost of this is that the `onAppear` triggers no longer trigger in snapshot tests, but that's a small price to pay to make sure the tests actually pass in CI.
schalkwijk pushed a commit to schalkwijk/swift-snapshot-testing that referenced this pull request Sep 7, 2020
This way, we don't accidentally hit bad transition points in tests, as per pointfreeco#366. Basically, because we're necessarily running our tests inside of a hosted application, the snapshot library incorrectly creates yet another window in which to run tests, which confuses the ... iOS runtime? or something and causes the app to queue up a bunch of appear / disappear transitions that clog up the main thread until they just halt the entire app, causing the app to stop for a solid 20-30 seconds, which explains timeout issues we're having in CI and occasionally locally.

The cost of this is that the `onAppear` triggers no longer trigger in snapshot tests, but that's a small price to pay to make sure the tests actually pass in CI.
schalkwijk pushed a commit to schalkwijk/swift-snapshot-testing that referenced this pull request Sep 7, 2020
This way, we don't accidentally hit bad transition points in tests, as per pointfreeco#366. Basically, because we're necessarily running our tests inside of a hosted application, the snapshot library incorrectly creates yet another window in which to run tests, which confuses the ... iOS runtime? or something and causes the app to queue up a bunch of appear / disappear transitions that clog up the main thread until they just halt the entire app, causing the app to stop for a solid 20-30 seconds, which explains timeout issues we're having in CI and occasionally locally.

The cost of this is that the `onAppear` triggers no longer trigger in snapshot tests, but that's a small price to pay to make sure the tests actually pass in CI.
@zenangst
Copy link
Author

@stephencelis

i3K1nMkuEtBeR

@dmitrysimkin
Copy link

Please merge, I also encounter this issue

@zenangst
Copy link
Author

I merged this with main now 😎

@okankocyigit
Copy link

@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 :)

@krzysztofpawski
Copy link
Contributor

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

tahirmt added a commit to tahirmt/swift-snapshot-testing that referenced this pull request Sep 22, 2022
tahirmt added a commit to tahirmt/swift-snapshot-testing that referenced this pull request Sep 22, 2022
thedavidharris pushed a commit to thedavidharris/swift-snapshot-testing that referenced this pull request Mar 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants