Thread
#multiplatform
    b

    basher

    3 years ago
    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
    p

    pardom

    3 years ago
    b

    basher

    3 years ago
    Huh. I guess we only build for JVM and native, which explains why that works
    Sorry!
    p

    pardom

    3 years ago
    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

    nestserau

    3 years ago
    You can reimplement runBlocking: common
    expect fun <T> runBlocking(block: suspend () -> T): T
    ios
    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
    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

    jw

    3 years ago
    the common pattern is to expect/actual it
    russhwolf

    russhwolf

    3 years ago
    If you don't need js, you can add your own expect/actual declarations to get runblocking in jvm/native.
    j

    jw

    3 years ago
    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
    russhwolf

    russhwolf

    3 years ago
    Yeah the ticket for
    @Test suspend
    has more details on workarounds: https://youtrack.jetbrains.com/issue/KT-22228
    j

    jw

    3 years ago
    1.3.30 🤞