Running Tests With Jest


#1

Currently using this Jest example. Is there a way to get the package.json file generated in Gradle? Below is the contents of the build file (build.gradle.kts):

import org.jetbrains.kotlin.gradle.tasks.Kotlin2JsCompile
import com.moowork.gradle.node.NodeExtension
import com.moowork.gradle.node.npm.NpmTask
import com.moowork.gradle.node.task.NodeTask

group = "org.example"
version = "0.1-SNAPSHOT"

buildscript {
    var kotlinVer: String by extra
    var nodeVer: String by extra

    kotlinVer = "1.2.0"
    nodeVer = "1.2.0"
    repositories {
        jcenter()
        mavenCentral()
        maven {
            url = uri("https://plugins.gradle.org/m2")
        }
    }

    dependencies {
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVer")
        classpath("com.moowork.gradle:gradle-node-plugin:$nodeVer")
    }
}

val kotlinVer: String by extra

apply {
    plugin("kotlin2js")
    plugin("com.moowork.node")
}

configure<NodeExtension> {
    download = true
}

repositories {
    jcenter()
    mavenCentral()
}

dependencies {
    "compile"("org.jetbrains.kotlin:kotlin-stdlib-js:$kotlinVer")
    "testCompile"("org.jetbrains.kotlin:kotlin-test-js:$kotlinVer")
}

val compileKotlin2Js by tasks.getting(Kotlin2JsCompile::class) {
    kotlinOptions.moduleKind = "umd"
}
val compileTestKotlin2Js by tasks.getting(Kotlin2JsCompile::class) {
    kotlinOptions.moduleKind = "umd"
}
val populateNodeModules = task<Copy>("populateNodeModules") {
    from(compileKotlin2Js.destinationDir)
    configurations["testCompile"].forEach { f ->
        from(zipTree(f.absolutePath).matching { include("*.js") })
    }
    into("$buildDir/node_modules")
}
val installJest = task<NpmTask>("installJest") {
    setNpmCommand("install", "--save-dev", "jest")
}
val runJest = task<NodeTask>("runJest") {
    dependsOn(compileTestKotlin2Js, populateNodeModules, installJest)
    setScript(file("node_modules/jest/bin/jest.js"))
    addArgs(compileKotlin2Js.outputFile)
}

tasks.getting(Test::class) {
    dependsOn(runJest)
}

#2

Creating a file from a build.gradle.kts should not be different from the usual Kotlin/JVM: https://stackoverflow.com/questions/35444264/how-do-i-write-to-a-file-in-kotlin/35445704