Registering Kotlin JS Tests With Karma?


#1

Trying to do some basic testing for a Kotlin multi-platform library called Web Scene Core (just the js module) , and have been going round in circles with no tests being registered by Karma. Thinking that there might be something missing/incorrect in the build file (using Gradle Kotlin DSL). Below are the contents of the build file (modified version):

import org.jetbrains.kotlin.gradle.tasks.Kotlin2JsCompile
import com.craigburke.gradle.KarmaModuleExtension
import com.moowork.gradle.node.NodeExtension

val moduleName = "$rootProjectName-${project.name}"

plugins {
    `maven-publish`
    id(PluginId.karma) version Version.karmaPlugin
}

repositories {
    jcenter()
    mavenCentral()
}

buildscript {
    repositories {
        mavenCentral()
        jcenter()
    }

    dependencies {
        classpath(kotlin(module = "gradle-plugin", version = Version.kotlin))
    }
}

apply {
    plugin(PluginId.kotlinPlatformJs)
    plugin(PluginId.kotlin2Js)
    from("${rootProject.rootDir.absolutePath}/publishing.gradle")
}

dependencies {
    "expectedBy"(project(":common"))
    "compile"(kotlin(module = "stdlib-js", version = Version.kotlin))
    "testCompile"(Dependency.kotlinTestJs)
}

val libDir = "$buildDir/lib"
val testDir = "$buildDir/test"
val compileKotlin2Js by tasks.getting(Kotlin2JsCompile::class) {
    val destDir = "${projectDir.absolutePath}/web"
    kotlinOptions {
        outputFile = "$destDir/js/$rootProjectName-$version.js"
        sourceMap = true
        moduleKind = "umd"
    }
    doFirst { File(destDir).deleteRecursively() }
}
val compileTestKotlin2Js by tasks.getting(Kotlin2JsCompile::class) {
    kotlinOptions {
        sourceMap = true
        moduleKind = "umd"
    }
}

configure<NodeExtension> {
    version = Version.node
    download = true
}

configure<KarmaModuleExtension> {
    dependencies(mutableListOf("mocha@${Version.mocha}"))
    frameworks = mutableListOf("mocha")
    browsers = mutableListOf("Chrome")
    files("$libDir/*.js", "$testDir/*.js")
}

val populateNodeModules by tasks.creating(Copy::class) {
    dependsOn(compileKotlin2Js)
    configurations["testCompile"].forEach {
        from(zipTree(it.absolutePath).matching {
            include("*.js", "*.js.map")
        })
    }
    into(libDir)
}
val generateTestDir by tasks.creating(Sync::class) {
    dependsOn(compileTestKotlin2Js)
    from((compileKotlin2Js as Kotlin2JsCompile).outputFile.absolutePath,
        (compileTestKotlin2Js as Kotlin2JsCompile).outputFile.absolutePath)
    into(testDir)
}
val karmaRun by tasks.getting {
    dependsOn(compileTestKotlin2Js, populateNodeModules, generateTestDir)
}
val test by tasks.getting {
    dependsOn(karmaRun)
}
val clean by tasks.getting {
    dependsOn("karmaClean")
}
val createSourceJar by tasks.creating(Jar::class) {
    baseName = moduleName
    dependsOn("classes")
    classifier = "sources"
    from("src/main/kotlin")
}
val jar by tasks.getting(Jar::class) {
    baseName = moduleName
}

task("createAllJarFiles") {
    dependsOn("jar", createSourceJar)
    println("Creating $moduleName JAR files (library, sources and documentation)...")
    doLast { println("Finished creating JAR files.") }
}

Here are the results after running the karmaRun task:

> Task :js:karmaRun
Chrome 67.0.3396 (Linux 0.0.0): Executed 0 of 0 ERROR (0.001 secs / 0 secs)

> Task :js:karmaRun FAILED

FAILURE: Build failed with an exception.

Noticed with running the karmaRun task that a Chrome window briefly appears for about 2 seconds displaying a green connected banner before the window disappears. How are Kotlin JS tests registered with Karma?