<https://kotlinlang.slack.com/archives/C1CFAFJSK/p...
# multiplatform
b
I think it is in common. I'm currently running tests using it
I have fun into IDE issues before getting it to be recognized
b
Huh. I guess we only build for JVM and native, which explains why that works
Sorry!
p
Using an instance of
CoroutineDispatcher
local to the test class did the trick: https://gist.github.com/pardom/74a9f1abc17688badf6b8dc06e13b1d2#file-runtimetest-kt-L13-L17
n
You can reimplement runBlocking: common
Copy code
expect fun <T> runBlocking(block: suspend () -> T): T
ios
Copy code
actual fun <T> runBlocking(block: suspend () -> T): T {
    val expectation = Expectation<T>()

    GlobalScope.launch(MainRunLoopDispatcher) {
        expectation.fulfill(block.invoke())
    }

    return expectation.wait() ?: throw RuntimeException("runBlocking failed")
}

private object MainRunLoopDispatcher : CoroutineDispatcher() {
    override fun dispatch(context: CoroutineContext, block: Runnable) {
        NSRunLoop.mainRunLoop().performBlock {
            block.run()
        }
    }
}
android
Copy code
actual fun <T> runBlocking(block: suspend () -> T): T = kotlinx.coroutines.runBlocking { block.invoke() }
I’m not the original author, but I don’t remember the original author unfortunately. Hope that helps.
j
the common pattern is to expect/actual it
r
If you don't need js, you can add your own expect/actual declarations to get runblocking in jvm/native.
j
even in JS is still works because you can return a promise from the test APIs
and the runner will wait
this will all be obviated once
@Test suspend fun
lands though
r
Yeah the ticket for
@Test suspend
has more details on workarounds: https://youtrack.jetbrains.com/issue/KT-22228
j
1.3.30 🤞
🤞 5
👌 3