pointfreeco / swift-snapshot-testing

📸 Delightful Swift snapshot testing.

Home Page:https://www.pointfree.co/episodes/ep41-a-tour-of-snapshot-testing

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

mpivchev opened this issue · comments

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

  • swift-snapshot-testing version 1.11.0
  • Xcode 14.3
  • Swift 5
  • OS: iOS 16.4

You could check this workaround: #368 (comment)