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

thanksforallthefish

04/22/2021, 1:17 PM
very dumb question most probably, but I am experimenting for the first time with mpp and would like to write tests in javascript, so that I can verify how the code I wrote in kotlin should be used on the other side (for now I am interested in js and jvm). for instance, in
commonMain
I have
Copy code
package ...

class HateoasObjectImpl(val any: Map<String, *>) : HateoasObject {

  override fun getLink(name: String): Link {
    TODO("Not yet implemented")
  }

  override fun getHint(name: String): Hint {
    TODO("Not yet implemented")
  }

  override fun getLinkAndHint(name: String): Pair<Link, Hint> {
    TODO("Not yet implemented")
  }
}
in
jsTest
can I somehow reference this code in a
js
file? or that is not possible, since js code is generated when running gradle?
a

Anton Lakotka [JB]

04/29/2021, 8:57 AM
Hi Marco, just scrolled the multiplatform channel and spotted your avatar 🙂 . And as former colleague I feel that I should help you 🙂 In short, it should be possible to reference any common code in more platform-specific sourcesets. If you are still having troubles Could you please share your gradle configuration so I can try help you how to set it up.
And I’m also wondering what do you mean by referencing in a JS file? Do you want to call this code from JS? it should be possible in any way: https://kotlinlang.org/docs/js-to-kotlin-interop.html https://kotlinlang.org/docs/js-interop.html
t

thanksforallthefish

04/29/2021, 12:43 PM
you spotted correctly sir, it is indeed me 😄 yes, I mean I would like to use the generated code in a test, because I mostly never did frontend and I have no idea whether the js generated makes sense. not in the sense the code generated is bad, more to see how it would look when consume from javascript. I put the experiment on hold for now, too many new things all at once, but thanks for the heads up
and fyi, gradle file is very basic, mostly autogenerated from idea:
Copy code
plugins {
  kotlin("multiplatform") version "1.4.32"
  kotlin("plugin.serialization") version "1.4.32"
  id("maven-publish")
}

version = "1.0-SNAPSHOT"

val ktor_version = "1.5.3"

repositories {
  mavenCentral()
}

dependencies {
}

kotlin {
  jvm {
    compilations.all {
      kotlinOptions.jvmTarget = "11"
    }
    testRuns["test"].executionTask.configure {
      useJUnit()
    }
  }
  js(IR) {
    useCommonJs()
    browser {
      commonWebpackConfig {
        cssSupport.enabled = true
      }
    }
  }
  val hostOs = System.getProperty("os.name")
  val isMingwX64 = hostOs.startsWith("Windows")
  val nativeTarget = when {
    hostOs == "Mac OS X" -> macosX64("native")
    hostOs == "Linux" -> linuxX64("native")
    isMingwX64 -> mingwX64("native")
    else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
  }


  sourceSets {
    val commonMain by getting {
      dependencies {
        implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.1.0")
        implementation("io.ktor:ktor-client-core:$ktor_version")
        implementation("io.ktor:ktor-client-serialization:$ktor_version")
//        implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3")
//        implementation("org.springframework.hateoas:spring-hateoas:1.3.0")
      }
    }
    val commonTest by getting {
      dependencies {
        implementation(kotlin("test-common"))
        implementation(kotlin("test-annotations-common"))
        implementation("io.ktor:ktor-client-mock:$ktor_version")
      }
    }
    val jvmMain by getting {
      dependencies {
        implementation(kotlin("reflect"))
        implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.11.4")
        implementation("io.ktor:ktor-client-cio:$ktor_version")
        implementation("io.ktor:ktor-client-jackson:$ktor_version")
      }
    }
    val jvmTest by getting {
      dependencies {
        implementation(kotlin("test-junit"))
        implementation("com.github.tomakehurst:wiremock-jre8:2.27.2")
      }
    }
    val jsMain by getting {
      dependencies {
        implementation("io.ktor:ktor-client-js:$ktor_version")
      }
    }
    val jsTest by getting {
      dependencies {
        implementation(kotlin("test-js"))
      }
    }
    val nativeMain by getting
    val nativeTest by getting
  }
}
a

Anton Lakotka [JB]

04/29/2021, 3:51 PM
So you want to call some kotlin code from JS in browser? So first of all you would need to publish the JS code in some way. So then you can consume it in pure JS project. https://kotlinlang.org/docs/js-modules.html If you can specify what exactly is your goal so maybe I can give you more concrete steps how to achieve it. For example you might want to build some Client Library. That can be used on both JS and JVM sides to call some backend API over HTTP.