2

I'm working on a Kotlin multi-platform project, and I need my JS tests to run on Node.js but with custom command line arguments (specifically I need node to run with the --expose-gc flag, because some tests need to trigger garbage collection).

Looking at the documentation for the Gradle Kotlin JS DSL I didn't find any mention of how to do that; does anyone know whether it's at all possible and how?

Tom
  • 4,910
  • 5
  • 33
  • 48

1 Answers1

0

Unfortunately can not answer your question directly, but there is some suggestion to help you with reverse engineering.

Let's start from some example. We have Gradle tasks to run our project using webpack's dev server such as browserDevelopmentRun, browserProductionRun (not sure if multi-platform projects have it, but JS projects do). We can add:

println(tasks.named("browserProductionRun").get().javaClass)

to build.gradle.kts to find out the exact class used for this task. When we sync Gradle, it outputs:

org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpack_Decorated

Now we know the exact class of this task so we can investigate its API. The auto completion or navigating inside of the KotlinWebpack class helps us to find out that it has a helpful nodeArgs property to configure NodeJS arguments for it, so we can set them, for example:

tasks.named("browserProductionRun", org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpack::class).get().nodeArgs.add("--trace-deprecation")

Getting back to your question.

In your case I guess you need to investigate the browserTest task. Let's get some info about it by adding:

println(tasks.named("browserTest").get().javaClass)

to build.gradle.kts - a-ha - it seems to be of the org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest_Decorated type. Let's check what's inside. Open KotlinJsTest.kt somehow - for example by typing its name into the window being opened by CMD + Shift + O (make sure to select "All Places" here) or just by typing its name somewhere in build.gradle.kts and navigating inside it.

The only interesting thing I see inside this open class is the following block:

    override fun createTestExecutionSpec(): TCServiceMessagesTestExecutionSpec {
        val forkOptions = DefaultProcessForkOptions(fileResolver)
        forkOptions.workingDir = npmProject.dir
        forkOptions.executable = nodeJs.requireConfigured().nodeExecutable

        val nodeJsArgs = mutableListOf<String>()

        return testFramework!!.createTestExecutionSpec(
            task = this,
            forkOptions = forkOptions,
            nodeJsArgs = nodeJsArgs,
            debug = debug
        )
    }

So maybe it can work out to create your own extension of this class, override its createTestExecutionSpec method and provide nodeJsArgs as you need inside it. After that you'll be needing to declare another Gradle task to launch tests inside build.gradle.kts which will use this new extended class.

Andrei K.
  • 534
  • 7
  • 20