I have a Multiplatform Project (js, jvm, native). ...
# multiplatform
j
I have a Multiplatform Project (js, jvm, native). What is the best approach if I want to have tests for common code? Can I configure common-test to run tests on the JVM, or should I put all of that inside the jvmTest module? Is there any good "optimal solution" example on github that I can follow?
s
I have been putting my tests into the commonTest sourceSet. You can run these against JVM, JS and Native. The two issues I have run into is that I have not been able to get a mocking library to run under commonTest, the other is runBlocking/runBlockingTest is not available yet. To test coroutine/suspend code, other devs on slack pointed me to the below code which has been a helpful workaround for now.
Copy code
// put in commonTest
expect fun runTest(block: suspend (scope : CoroutineScope) -> Unit)
// put in jsTest
actual fun runTest(block: suspend (scope : CoroutineScope) -> Unit): dynamic = GlobalScope.promise { block(this) }
// put in jvmTest & nativeTest
actual fun runTest(block: suspend (scope : CoroutineScope) -> Unit) = runBlocking { block(this) }
Then just use runTest in place of runBlocking/runBlockingTest in your tests.
Copy code
@Test
fun some_test() = runTest {
    assertEquals(5, someResult())
}
I don't have a public repo available right now, but if you are using IntelliJ, the new project wizard produces the base that I have been working from that has the commonTest sourceSet.
j
I get the "Kotlin not configured" when putting it in the commonTest sourceSet How did you fix that? I do not want to configure the commonTest module in such a way that it only works on the JVM and runs on it. Did you just modify the run configuration so it uses jvm? Do you have an example for me ready on github? This is the project in question https://github.com/jaqxues/ProLogicis/ The tests are in the core module, and I want the tests to be in the commonTest source set
s
Here is a repo you can check out. I used the Kotlin Multiplatform Library template to generate it. Use the KMP template that is the closest match to what you are trying to build. https://github.com/scottnj/KMPLibraryHelloWorld The 2nd screenshot is from another project but should work for this one also.
j
Thank you
It did end up working for me after a few days of wondering why it worked for that project and not for mine. Reinstalled my entire system yesterday and now it works after a fresh git clone.