Reputation: 2220
I have a multiplatform kotlin project that compiles to JS and JVM, with the following build config:
common - build.gradle
plugins {
id 'kotlin-platform-common' version '1.3.10'
}
repositories {
mavenCentral()
jcenter()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-common:1.3.10"
compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.0.1'
testCompile "org.jetbrains.kotlin:kotlin-test-annotations-common"
testCompile "org.jetbrains.kotlin:kotlin-test-common"
}
js - build.gradle
plugins {
id 'kotlin-platform-js' version '1.3.10'
}
repositories {
mavenCentral()
jcenter()
}
dependencies {
expectedBy project(":Lib-common")
compile "org.jetbrains.kotlin:kotlin-stdlib-js:1.3.10"
compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core-js:1.0.1'
testCompile "org.jetbrains.kotlin:kotlin-test"
testCompile "org.jetbrains.kotlin:kotlin-test-js"
}
compileKotlin2Js {
kotlinOptions.outputFile = "${projectDir}/output/lib."
kotlinOptions.moduleKind = "commonjs"
kotlinOptions.metaInfo = false
}
compileTestKotlin2Js {
kotlinOptions.outputFile = "${projectDir}/output/lib_test.js"
kotlinOptions.moduleKind = "commonjs"
kotlinOptions.metaInfo = false
}
jvm - build.gradle
plugins {
id 'kotlin-platform-jvm' version '1.3.10'
}
repositories {
mavenCentral()
jcenter()
}
dependencies {
expectedBy project(":Lib-common")
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.10"
compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1'
compile 'org.apache.httpcomponents:httpcore:4.4.11'
compile 'org.apache.httpcomponents:httpclient:4.5.7'
compile 'org.apache.httpcomponents:httpmime:4.5.7'
testCompile "junit:junit:4.12"
testCompile "org.jetbrains.kotlin:kotlin-test"
testCompile "org.jetbrains.kotlin:kotlin-test-junit"
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
sourceCompatibility = "1.8"
I have a test in the common module that runs as expected on the JVM. The JS module successfully compiles and generates a js file through the compileKotlin2Js
task, however, when I run compileTestKotlin2Js
the test class in the common module cannot find the class it is testing. This is the failure when I try to run compileTestKotlin2Js
:
> Task :Lib-js:classes
e: <location>\Lib\Lib-common\src\test\kotlin\mypackage\ClientTest.kt: (3, 45): Unresolved reference: Client
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':Lib-js:compileTestKotlin2Js'.
> Compilation error. See log for more details
Upvotes: 0
Views: 389
Reputation: 23105
kotlin-platform-js
gradle plugin does not create tasks to run tests, it only compiles Kotlin code in main and test source sets, so you need to setup all testing infrastructure for JS by yourself.
I recommend switching to the new multiplatform project authoring approach when all code resides in a single module which has kotlin-mutliplatform
plugin applied. That plugin can have JS test tasks configured out-of-the-box.
The build.gradle file of such project looks like:
plugins {
id 'org.jetbrains.kotlin.multiplatform' version '1.3.50'
}
repositories {
mavenCentral()
}
group 'com.example'
version '0.0.1'
kotlin {
jvm()
js {
nodejs { // target and run tests on node.js
}
}
sourceSets {
commonMain {
dependencies {
implementation kotlin('stdlib-common')
}
}
commonTest {
dependencies {
implementation kotlin('test-common')
implementation kotlin('test-annotations-common')
}
}
jvmMain {
dependencies {
implementation kotlin('stdlib-jdk8')
}
}
jvmTest {
dependencies {
implementation kotlin('test')
implementation kotlin('test-junit')
}
}
jsMain {
dependencies {
implementation kotlin('stdlib-js')
}
}
jsTest {
dependencies {
implementation kotlin('test-js')
}
}
}
}
You can read more about developing multiplatform projects here: https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#building-multiplatform-projects-with-gradle
Upvotes: 1