I'd love to be able to get a @Composable
context running in the androidTest
target of my common project, in order to test higher-order components that reside in commonMain
, such as ContentLocalProviders and layouts. Something like:
@Test fun testSomethingComposable() = runComposeTest {
@Composable fun <M> buildMutableState(model: M) { /* ... */ }
assertNotNull { buildMutableState(initialState).value }
}
I began with expect fun runComposeTest(content: @Composable ()->Unit)
inside of commonTest
, but only the jvmTest
version works. The createComposeRule().setContent {}
function offered by AndroidX only works within an instrumented test.
Any other way around this? Meanwhile, I've just pushed my tests down to jvmTest
so that I can move forward.
The jvmTest
version:
import androidx.compose.ui.window.application
actual fun runComposeTest(content: @Composable () -> Unit) = runTest {
application(false) {content}
}
But the following fails in androidTest
:
actual fun runComposeTest(content: @Composable () -> Unit) = runTest {
val rule = createComposeRule()
rule.setContent(content)
}
... For somewhat obvious reasons.