Open lukeredpath opened 9 months ago
I did some exploration, but I haven't found a great solution yet.
I tried making a custom macro that generates similar code to the #Preview
macro from Apple, but Xcode won't show the previews.
Even just using a typealias
to the exact same macro doesn't show the Xcode preview canvas, so I'm guessing Xcode is looking for #Preview
exactly to decide to show the preview or not.
If I make a new macro that points to the same exact implementation from Apple and expands to equivalent code Xcode still won't render the MyPreview
preview
Is Prefire
able to do it because they're doing it outside of Xcode using auto-generated code?
https://github.com/BarredEwe/Prefire/compare/main...preview_macro_supporting
@sureshjoshi I wasn't very familiar with Prefire
before, but I just spent some time looking at the project. Pretty neat stuff!
Looking at the current code my understanding is that they don't support the #Preview
macro either. Their stencil file searches for any type conforming to PrefireProvider
, which can't be applied to the code generated by #Preview
.
The same is roughly true for PreviewSnapshots
with respect to Xcode 15. Everything works fine for PreviewSnapshots
as long as you're using the PreviewProvider
version of previews rather than the #Preview
version.
Edit: Wait, I was looking at main
. You linked to a branch. I'll take a look at that too.
Is Prefire able to do it because they're doing it outside of Xcode using auto-generated code?
@sureshjoshi: yes, that looks like what they're doing. They're searching through the temporary directory that Xcode stores the macro generated code in to look for the macro output and then manually parsing that output. It's pretty clever.
That approach isn't a good fit for PreviewSnapshots
because we're using for an opt-in model without generated code. PreviewSnapshots
decided to prioritize having a relatively simple implementation that's hopefully easy to trace through and understand over automatic coverage via generated code. Both approaches are perfectly valid and I'm glad to see that there are multiple choices to fit with individual teams priorities!
Thanks for the quick reply!
I like what preview-snapshots can do, but need to go back and try to re-factor like, a two hundred components away from #Preview
😢
@sureshjoshi You could also take a look at https://github.com/EmergeTools/SnapshotPreviews-iOS to see if that fits your needs
Neat, thanks - I'd never seen that before
I love the idea of generating snapshot tests from previews but before I take the plunge and check this library out, I was wondering if you'd explored how this integrates with the new #Preview macro in Xcode 15?