Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI preview breaks with error: <unittest.file> not found in any targets

Here on my current configuration (Big Sur 11.1 (20C69), Xcode 12.3 (12C33)) I have an problem

withCouldNotFindInputNodeInBuiltGraphError<XcodeGraphSource, PreviewTranslationUnit>: WeeklyPlanViewModel_Tests.swift not found in any targets
WeeklyPlanViewModel_Tests.swift must belong to at least one target in the current scheme in order to use previews

and no of my changes bring me to get preview running again.

Clearing the project-build, deleting complete "DerivedData", also deactivating of this unittest-file (it is only this one available) and complete removing of unittest-target from the project: nothing helped to fix the preview-functionality.

What can I do in addition to get preview run again?

Interesting fact is, when creating an project new, copy all the files (without any tests) inside the project, and try out the preview, it works some time. But I cannot develop without testing. After git-commit and inserting some testing files, it works one-two times, then it stops. And no git-reverting to the previous commit, brings preview again to life.

like image 793
fanton Avatar asked Oct 27 '25 09:10

fanton


1 Answers

I managed to fix the issue. I think Xcode got confused somewhere down the line when I started creating separate groups inside my main Tests group to hold various test files.

How I fixed it:

  1. Rename the class. So for example, my test class was originally SomeClass. I renamed it to SomeClass2.

  2. Create a new test file using File > New > File and give it the original name of the class you renamed SomeClass

  3. Copy and paste your tests into that file.

  4. Delete the old test file.

This instantly fixed the issue for me. I guess something went wrong in Xcode and it couldn't locate the problematic file.

like image 130
LondonGuy Avatar answered Oct 30 '25 00:10

LondonGuy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!