Leandro Borges Ferreira
02/25/2025, 3:49 PMNo ViewModelStoreOwner was provided via LocalViewModelStoreOwner
When viewModels are instantiated by the app inside the test. The app works just fine, but the UI tests never run. I'm using runComposeUiTest
. Is this a known issue?zsmb
02/25/2025, 4:43 PMLocalViewModelStoreOwner
composition local that any ViewModels can be scoped to.
If you're using the nav library, its destinations each provide owners automatically. There's also "global" ones on each platform for the entry points of Compose content, here's the desktop one for example (on Android it's the Activity by default, etc).
If you're sure you want ViewModels to be instantiated like this in a test (as opposed to testing smaller parts, or without VMs), you can create your own VMStoreOwner implementation for tests and provide it with a CompositionLocalProvider
within setContent
, it shouldn't be too much code.Leandro Borges Ferreira
02/25/2025, 4:49 PMLeandro Borges Ferreira
02/25/2025, 4:49 PMzsmb
02/25/2025, 4:59 PMLeandro Borges Ferreira
02/25/2025, 5:00 PMLeandro Borges Ferreira
02/27/2025, 12:31 PMjava.lang.IllegalStateException: CompositionLocal LocalLifecycleOwner not present
at androidx.lifecycle.compose.LocalLifecycleOwnerKt$LocalLifecycleOwner$1.invoke(LocalLifecycleOwner.nonAndroid.kt:28)
at androidx.lifecycle.compose.LocalLifecycleOwnerKt$LocalLifecycleOwner$1.invoke(LocalLifecycleOwner.nonAndroid.kt:27)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:83)
at androidx.compose.runtime.LazyValueHolder.getCurrent(ValueHolders.kt:46)
at androidx.compose.runtime.LazyValueHolder.readValue(ValueHolders.kt:48)
at androidx.compose.runtime.CompositionLocalMapKt.read(CompositionLocalMap.kt:88)
at androidx.compose.runtime.ComposerImpl.consume(Composer.kt:2390)
at androidx.navigation.compose.NavHostKt.NavHost(NavHost.kt:831)
at androidx.navigation.compose.NavHostKt.NavHost(NavHost.kt:231)