rra/ilgonmic/fork-options-js-test

[Gradle, JS] Environment instead of fork options

[Gradle, JS] Add test on fork options

[Gradle, JS] Add fork options to kotlin js test

Merge-request: KT-MR-5793
Merged-by: Ilya Goncharov <Ilya.Goncharov@jetbrains.com>

^KT-51414 fixed
This commit is contained in:
Ilya Goncharov
2022-02-21 08:41:29 +00:00
committed by Space
parent 46c0c4f9d9
commit 2173ce77d5
7 changed files with 89 additions and 0 deletions
@@ -980,6 +980,18 @@ abstract class AbstractKotlin2JsGradlePluginIT(protected val irBackend: Boolean)
}
}
@DisplayName("NodeJs test with custom fork options")
@GradleTest
fun testNodeJsForkOptions(gradleVersion: GradleVersion) {
project("kotlin-js-nodejs-custom-node-module", gradleVersion) {
build("build") {
checkIrCompilationMessage()
assertTasksExecuted(":nodeTest")
}
}
}
@DisplayName("no dependencies from other modules are declared")
@GradleTest
fun testNoUnintendedDevDependencies(gradleVersion: GradleVersion) {
@@ -0,0 +1,26 @@
plugins {
id("org.jetbrains.kotlin.js")
}
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-js")
testImplementation("org.jetbrains.kotlin:kotlin-test-js")
}
repositories {
mavenLocal()
mavenCentral()
}
kotlin {
js {
useCommonJs()
binaries.executable()
nodejs {
testTask {
environment("hello", "hello")
environment("NODE_PATH", projectDir.absolutePath)
}
}
}
}
@@ -0,0 +1,5 @@
function foo() {
return "foo"
}
module.exports.foo = foo
@@ -0,0 +1,7 @@
{
"name": "custom",
"version": "1.0.0",
"main": "index.js",
"devDependencies": {
}
}
@@ -0,0 +1,4 @@
@file:JsModule("custom")
package foo
external fun foo(): String
@@ -0,0 +1,21 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
import kotlin.test.Test
import kotlin.test.assertEquals
class Tests {
@Test
fun test1() {
assertEquals("hello", process.env.hello)
}
@Test
fun test2() {
assertEquals("foo", foo.foo())
}
}
external val process: dynamic
@@ -9,6 +9,7 @@ import groovy.lang.Closure
import org.gradle.api.file.FileCollection
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.tasks.*
import org.gradle.process.ProcessForkOptions
import org.gradle.process.internal.DefaultProcessForkOptions
import org.gradle.util.ConfigureUtil
import org.jetbrains.kotlin.gradle.internal.testing.TCServiceMessagesTestExecutionSpec
@@ -22,6 +23,7 @@ import org.jetbrains.kotlin.gradle.targets.js.testing.mocha.KotlinMocha
import org.jetbrains.kotlin.gradle.tasks.KotlinTest
import org.jetbrains.kotlin.gradle.utils.getValue
import org.jetbrains.kotlin.gradle.utils.newFileProperty
import java.io.File
import javax.inject.Inject
open class KotlinJsTest
@@ -44,6 +46,9 @@ constructor(
private val projectPath = project.path
@Nested
var environment = mutableMapOf<String, String>()
@get:Internal
var testFramework: KotlinJsTestFramework? = null
set(value) {
@@ -133,12 +138,17 @@ constructor(
KotlinKarma(compilation, { services }, path),
body
)
fun useKarma(fn: Closure<*>) {
useKarma {
ConfigureUtil.configure(fn, this)
}
}
fun environment(key: String, value: String) {
this.environment[key] = value
}
private inline fun <T : KotlinJsTestFramework> use(runner: T, body: T.() -> Unit): T {
check(testFramework == null) {
"testFramework already configured for task ${this.path}"
@@ -160,6 +170,10 @@ constructor(
forkOptions.workingDir = npmProjectDir
forkOptions.executable = nodeExecutable
environment.forEach { (key, value) ->
forkOptions.environment(key, value)
}
val nodeJsArgs = mutableListOf<String>()
return testFramework!!.createTestExecutionSpec(