pointfreeco / swift-snapshot-testing

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

"No image could be generated for this view as its size was zero" for a SwiftUI view #738

Open mpivchev opened 1 year ago

mpivchev commented 1 year ago

Describe the bug This is the image that is generated from trying to snapshot a SwiftUI view:

image

I tested multiple views and this happens for all of them.

To Reproduce

  1. Write test function for a SwiftUI view
  2. Generated image is an error

This is the code in my test:

let contentView = NCCapabilitiesView(capabilitiesStatus: getCapabilitiesViewOOForPreview())
assertSnapshot(matching: contentView, as: .image)

The view shows just fine on Xcode preview and I call it the same way:

static var previews: some View {
    NCCapabilitiesView(capabilitiesStatus: getCapabilitiesViewOOForPreview())
}

The only way I can fix this is by specifying the frame size myself:

NCCapabilitiesView(capabilitiesStatus: getCapabilitiesViewOOForPreview()).frame(width: 300, height: 300)

I don't think this is how it's supposed to work however.

Expected behavior The snapshot image should generate properly from the SwiftUI view.

Environment

stamceidas commented 10 months ago

You could check this workaround: https://github.com/pointfreeco/swift-snapshot-testing/issues/368#issuecomment-698834243