I have a question about jetpack compose UI testin...
# compose
i
I have a question about jetpack compose UI testing. I use side effect (LaunchedEffect) in root of my Composition. Actually this side effect subscribes on flow (just
Boolean?
) to post new root in NavHostController. I use it to unauthorize user (when error 401 received from server, for example) or authorize user when it enter credentials. In app code it works perfect. But when i go through app with compose UI tests i am getting exception when user just authorized. Something like
You can perform this action (post new root in NavController) only in main thread
. When i dive into
LaunchedEffect
i found that it uses coroutines context from current composer [thread]. But for ui tests this current composer returns
TestCoroutinesContext
that use
Dispatchers.Default
instead of
Dispatchers.Main
. It leads that side effects crashes the app, if trying to access UI thread operations. How must i to act in this situations? Use somehow MainDispatcher directly in code (it is far from obvious)? Looks like there will be good approach to add to user the possibility to change composer dispatcher for UI tests
Copy code
@Composable
@NonRestartableComposable
@OptIn(InternalComposeApi::class)
fun LaunchedEffect(
    key1: Any?,
    block: suspend CoroutineScope.() -> Unit
) {
    val applyContext = currentComposer.applyCoroutineContext
    remember(key1) { LaunchedEffectImpl(applyContext, block) }
}