Testing/mocking in Kotlin/JS

Our application makes a call to a REST API using a Ktor client and we would like to mock this call by mocking either the Client or the HTTP call in order to avoid complicated testing infrastructure.

We have tried using Mockk and Kotest but it seems that all the mocking libraries (for example the ones mentioned here) target explicitly the JVM, but not the JS platform.

Do you know whether mocking for the JS platform is currently possible at all?


Did you try to use the MockEngine of Ktor? It seems to fulfill your needs.

val client = HttpClient(MockEngine) {
    engine {
        addHandler { request ->
            when (request.url.fullUrl) {
                "https://example.org/" -> {
                    val responseHeaders = headersOf("Content-Type" to listOf(ContentType.Text.Plain.toString()))
                    respond("Hello, world", headers = responseHeaders)
                else -> error("Unhandled ${request.url.fullUrl}")


1 Like