[Gradle, JS] Add idle run for debug node tests
Source maps can be loaded by NodeJS
This commit is contained in:
+34
-15
@@ -19,6 +19,7 @@ import org.jetbrains.kotlin.gradle.targets.js.npm.npmProject
|
|||||||
import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest
|
import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest
|
||||||
import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTestFramework
|
import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTestFramework
|
||||||
import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinTestRunnerCliArgs
|
import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinTestRunnerCliArgs
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
class KotlinMocha(override val compilation: KotlinJsCompilation) : KotlinJsTestFramework {
|
class KotlinMocha(override val compilation: KotlinJsCompilation) : KotlinJsTestFramework {
|
||||||
private val project: Project = compilation.target.project
|
private val project: Project = compilation.target.project
|
||||||
@@ -59,19 +60,31 @@ class KotlinMocha(override val compilation: KotlinJsCompilation) : KotlinJsTestF
|
|||||||
exclude = task.excludePatterns
|
exclude = task.excludePatterns
|
||||||
)
|
)
|
||||||
|
|
||||||
createAdapterJs(task)
|
|
||||||
|
|
||||||
val mocha = npmProject.require("mocha/bin/mocha")
|
val mocha = npmProject.require("mocha/bin/mocha")
|
||||||
val adapter = npmProject.require("./$ADAPTER_NODEJS")
|
|
||||||
|
val file = task.nodeModulesToLoad
|
||||||
|
.map { npmProject.require(it) }
|
||||||
|
.single()
|
||||||
|
|
||||||
|
val adapter = createAdapterJs(file, debug)
|
||||||
|
|
||||||
val args = mutableListOf(
|
val args = mutableListOf(
|
||||||
"--require",
|
"--require",
|
||||||
npmProject.require("source-map-support/register.js")
|
npmProject.require("source-map-support/register.js")
|
||||||
|
|
||||||
).apply {
|
).apply {
|
||||||
|
if (debug) {
|
||||||
|
// Idle run of tests to load file with source maps to enable break points
|
||||||
|
add("--require")
|
||||||
|
add(
|
||||||
|
npmProject.require("kotlin-test-js-runner/kotlin-test-nodejs-idle-runner.js")
|
||||||
|
)
|
||||||
|
add("--require")
|
||||||
|
add(file)
|
||||||
|
|
||||||
|
add("--inspect-brk")
|
||||||
|
}
|
||||||
add(mocha)
|
add(mocha)
|
||||||
addAll(cliArg("--inspect-brk", debug))
|
add(adapter.canonicalPath)
|
||||||
add(adapter)
|
|
||||||
addAll(cliArgs.toList())
|
addAll(cliArgs.toList())
|
||||||
addAll(cliArg("--reporter", "kotlin-test-js-runner/mocha-kotlin-reporter.js"))
|
addAll(cliArg("--reporter", "kotlin-test-js-runner/mocha-kotlin-reporter.js"))
|
||||||
addAll(cliArg("--timeout", timeout))
|
addAll(cliArg("--timeout", timeout))
|
||||||
@@ -85,27 +98,33 @@ class KotlinMocha(override val compilation: KotlinJsCompilation) : KotlinJsTestF
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun cliArg(cli: String, value: Boolean): List<String> {
|
|
||||||
return if (value) listOf(cli) else emptyList()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun cliArg(cli: String, value: String?): List<String> {
|
private fun cliArg(cli: String, value: String?): List<String> {
|
||||||
return value?.let { listOf(cli, it) } ?: emptyList()
|
return value?.let { listOf(cli, it) } ?: emptyList()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createAdapterJs(task: KotlinJsTest) {
|
private fun createAdapterJs(
|
||||||
|
file: String,
|
||||||
|
debug: Boolean
|
||||||
|
): File {
|
||||||
val npmProject = compilation.npmProject
|
val npmProject = compilation.npmProject
|
||||||
val file = task.nodeModulesToLoad
|
|
||||||
.map { npmProject.require(it) }
|
|
||||||
.single()
|
|
||||||
|
|
||||||
val adapterJs = npmProject.dir.resolve(ADAPTER_NODEJS)
|
val adapterJs = npmProject.dir.resolve(ADAPTER_NODEJS)
|
||||||
adapterJs.printWriter().use { writer ->
|
adapterJs.printWriter().use { writer ->
|
||||||
val adapter = npmProject.require("kotlin-test-js-runner/kotlin-test-nodejs-runner.js")
|
val adapter = npmProject.require("kotlin-test-js-runner/kotlin-test-nodejs-runner.js")
|
||||||
|
val escapedFile = file.jsQuoted()
|
||||||
|
|
||||||
|
if (debug) {
|
||||||
|
// Invalidate caches after idle run
|
||||||
|
writer.println("delete require.cache[require.resolve($escapedFile)]")
|
||||||
|
writer.println("delete require.cache[require.resolve('kotlin-test')]")
|
||||||
|
}
|
||||||
|
|
||||||
writer.println("require(${adapter.jsQuoted()})")
|
writer.println("require(${adapter.jsQuoted()})")
|
||||||
|
|
||||||
writer.println("module.exports = require(${file.jsQuoted()})")
|
writer.println("module.exports = require($escapedFile)")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return adapterJs
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ tasks {
|
|||||||
inputs.dir("src")
|
inputs.dir("src")
|
||||||
inputs.files(
|
inputs.files(
|
||||||
"nodejs.ts",
|
"nodejs.ts",
|
||||||
|
"nodejs-idle.ts",
|
||||||
"karma.ts",
|
"karma.ts",
|
||||||
"karma-kotlin-reporter.js",
|
"karma-kotlin-reporter.js",
|
||||||
"karma-debug-runner.js",
|
"karma-debug-runner.js",
|
||||||
|
|||||||
+15
@@ -0,0 +1,15 @@
|
|||||||
|
import {KotlinTestRunner} from "./src/KotlinTestRunner";
|
||||||
|
|
||||||
|
const kotlin_test = require('kotlin-test');
|
||||||
|
|
||||||
|
const nothingTest: KotlinTestRunner = {
|
||||||
|
suite(name: string, isIgnored: boolean, fn: () => void): void {
|
||||||
|
console.error("suite", name)
|
||||||
|
},
|
||||||
|
test(name: string, isIgnored: boolean, fn: () => void): void {
|
||||||
|
console.error("test", name)
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
kotlin_test.setAdapter(nothingTest);
|
||||||
@@ -16,6 +16,16 @@ export default [
|
|||||||
},
|
},
|
||||||
plugins: plugins()
|
plugins: plugins()
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
input: './nodejs-idle.ts',
|
||||||
|
output: {
|
||||||
|
file: 'lib/kotlin-test-nodejs-idle-runner.js',
|
||||||
|
format: 'cjs',
|
||||||
|
banner: '#!/usr/bin/env node',
|
||||||
|
sourcemap: true
|
||||||
|
},
|
||||||
|
plugins: plugins()
|
||||||
|
},
|
||||||
{
|
{
|
||||||
input: './karma.ts',
|
input: './karma.ts',
|
||||||
output: {
|
output: {
|
||||||
|
|||||||
Reference in New Issue
Block a user