Snapshot SwiftUI previews from CLI without changing source code

I've been looking for a way to get snapshots of SwiftUI previews without changing the source code.

My use case is that I'm trying to augment the capabilities of coding assistants by letting them "see" what they did (using unit test is generally a great feedback loop for those AI assistants). But this is a broader use case as SwiftUI Previews are often used for testing and any decent testing is available from the CLI and can be integrated in different systems.

In an ideal world a developper would be able to run something like this and get snapshots:

swift snapshot-previews --filter SignupPreview --output ./snapshots

just like I'm able to run

swift test --filter SignupTests  --xunit-output ./test-output

I know this is not possible, and that any library out there for snapshotting SwiftUI Previews requires some direct integration and modification to the source code. I also know that while core to our development workflows, SwiftUI and its previews are not open source and developed privately by Apple.

So... what would folks recommend as the best solution to make this happen? I've been hesitating between three different directions:

  • copy the source code to a tmp directory, do the required rewritting there without changing the original source code, and leverage an existing 3rd party solution to do the snapshotting. Doesn't feel great but is likely to work while being brittle.
  • find out if it is possible to do some injection. Into what? The preview needs a running process, but a swift build/test doesn't create one afaik. Maybe it's the opposite where the code to be snapshotted is injected into a host snapshotter process. Not sure it can work but maybe good.
  • give up, it's a lost battle! The easiest option.

It seems that Apple does some code rewritting to support Previews, at least the hot reloading and private imports. Other than that I'm really out of my confort zone and I know little about those systems.

Have you thought about this at all since?

The widespread adoption of intelligent agentic systems based on LLMs like claude code has - I’m certain - others thinking about this too.

Unless you are developing on a magically fast desktop, the build + launch cycle is PAINFULLY slow for claude code to iterate. It’s also sometimes difficult for it to figure out how to navigate.

A good way to do this would be a 2x+ developer productivity benefit for me.

I have still not found a good solution. I 100% agree that such a solution would be even more useful now than in the pre-llm era. When you see what web developers can do with a browser embedded in the IDE and visible to the AI agent, it makes you want for a similar dev exp.

And even outside of AI, I’d love to have snapshotting in my CI that just keeps track of any preview I’ve taken time to define in my code.

I looked into it slightly more in the month after my last post - the way that apple generates previews behind the scenes is both much more complex and also much simpler than you might expect.

It does still do a full simulator boot & build behind the scenes, but there's a lot of magic inserted (with the functionality in private frameworks) to enable the runtime hot swapping of the previewed components themselves.

I'm still shocked that big ol apple didn't build the xcodebuildmcp, that it was up to a third party to do it! It would be much easier for them to add integration points with xcode than it is for 3rd party devs to try to hack up solutions.