https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
c

CLOVIS

02/27/2020, 3:07 PM
Is there a good quality guide on how to actually setup unit testing in Kotlin/JS for a multiplatform library? I've been following SO answers & stuff but the best I could find is 2 years old and very imprecise.
r

russhwolf

02/27/2020, 3:21 PM
Is it not working for you out-of-the-box? JS updates since 1.3.40 changed the plugin setup significantly so a lot of stuff is there by default now. Probably just need a
browser()
or
nodejs()
call in your
js{}
target block.
For docs, multiplatform setup isn't very different from the js-specific setup in https://kotlinlang.org/docs/reference/js-project-setup.html, except you do your js stuff in
js
instead of
target
and your dependencies move to the standard mpp location instead of top-level
j

Jeff

02/27/2020, 4:48 PM
@russhwolf I am trying to do testing with coroutines in JS.
Copy code
internal actual fun <T> runTest(block: suspend () -> T): dynamic {
    return promise { block() }
}
promise isn't being resolved. Should I add a library?
r

russhwolf

02/27/2020, 5:01 PM
For coroutine testing in js there's a setup descrubed in this youtrack ticket: https://youtrack.jetbrains.com/issue/KT-22228
Oh that's what you're doing
Not sure off-hand why that wouldn't work for you but make sure you have the kotlinx.coroutines dependencies added correctly
r

russhwolf

02/27/2020, 5:04 PM
the js stuff is all commented-out there
and you'll need
kotlinx-coroutines-core-js
in your js sources when you uncomment it
j

Jeff

02/27/2020, 5:07 PM
Sorry. Check it again. I've pushed the latest code. It's not working even with that dependency.
r

russhwolf

02/27/2020, 5:09 PM
You need core-js, not core-common
We've also drifted way off of the original thread here. Sorry Clovis
j

Jeff

02/27/2020, 5:10 PM
Copy code
val jsTest by getting {
    dependencies {
        implementation(kotlin("test-js"))

        api("io.ktor:ktor-client-mock-js:$ktorVersion")
        api("org.jetbrains.kotlinx:kotlinx-coroutines-core-js:$coroutinesVersion")
    }
}
Its core-js
The only promise that comes up is Promise with capital P. But the one I am trying to use is small p
r

russhwolf

02/27/2020, 5:14 PM
I'm not sure then. Sorry.
c

CLOVIS

02/27/2020, 5:36 PM
Thanks a lot @russhwolf, I didn't realize you could do that... Glad the ecosystem is becoming so simple!
j

Jeff

02/27/2020, 5:41 PM
Thanks. Got it to work!
Copy code
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.promise

internal actual fun <T> runTest(block: suspend () -> T): dynamic {
    return GlobalScope.promise { block() }
}