[JS] Implement strategy with adapterTransformer on global object to mutate adapter of kotlin-test

This commit is contained in:
Ilya Goncharov
2021-11-08 20:47:28 +03:00
committed by Space
parent 272081d8cf
commit 7aef45d5cf
4 changed files with 62 additions and 45 deletions
@@ -65,12 +65,27 @@ internal fun adapter(): FrameworkAdapter {
return result return result
} }
@JsName("kotlinTest")
external val kotlinTestNamespace: KotlinTestNamespace
internal fun detectAdapter() = when { external interface KotlinTestNamespace {
val adapterTransformer: ((FrameworkAdapter) -> FrameworkAdapter)?
}
internal fun detectAdapter(): FrameworkAdapter {
val frameworkAdapter = when {
isQUnit() -> QUnitAdapter() isQUnit() -> QUnitAdapter()
isJasmine() -> JasmineLikeAdapter() isJasmine() -> JasmineLikeAdapter()
else -> BareAdapter() else -> BareAdapter()
} }
return if (jsTypeOf(kotlinTestNamespace) != "undefined") {
val adapterTransform = kotlinTestNamespace
.adapterTransformer
if (adapterTransform !== null) {
adapterTransform(frameworkAdapter)
} else frameworkAdapter
} else frameworkAdapter
}
internal val NAME_TO_ADAPTER: Map<String, () -> FrameworkAdapter> = mapOf( internal val NAME_TO_ADAPTER: Map<String, () -> FrameworkAdapter> = mapOf(
"qunit" to ::QUnitAdapter, "qunit" to ::QUnitAdapter,
+7 -10
View File
@@ -5,14 +5,8 @@
import {CliArgsParser, getDefaultCliDescription} from "./src/CliArgsParser"; import {CliArgsParser, getDefaultCliDescription} from "./src/CliArgsParser";
import {runWithFilteringAndConsoleAdapters} from "./src/Adapter"; import {runWithFilteringAndConsoleAdapters} from "./src/Adapter";
import {KotlinTestRunner} from "./src/KotlinTestRunner";
let kotlin_test
try {
kotlin_test = require('kotlin-test');
} catch {
}
if (kotlin_test) {
const parser = new CliArgsParser( const parser = new CliArgsParser(
getDefaultCliDescription(), getDefaultCliDescription(),
(exitCode) => { (exitCode) => {
@@ -21,12 +15,15 @@ if (kotlin_test) {
); );
const untypedArgs = parser.parse(window.__karma__.config.args); const untypedArgs = parser.parse(window.__karma__.config.args);
const initialAdapter = kotlin_test.kotlin.test.detectAdapter_8be2vx$(); const adapterTransformer: (current: KotlinTestRunner) => KotlinTestRunner = current =>
kotlin_test.setAdapter(runWithFilteringAndConsoleAdapters(initialAdapter, untypedArgs)); runWithFilteringAndConsoleAdapters(current, untypedArgs);
window.kotlinTest = {
adapterTransformer: adapterTransformer
}
const resultFun = window.__karma__.result; const resultFun = window.__karma__.result;
window.__karma__.result = function (result) { window.__karma__.result = function (result) {
console.log(`--END_KOTLIN_TEST--\n${JSON.stringify(result)}`); console.log(`--END_KOTLIN_TEST--\n${JSON.stringify(result)}`);
resultFun(result) resultFun(result)
}; };
}
@@ -1,19 +1,16 @@
import {CliArgsParser, getDefaultCliDescription} from "./src/CliArgsParser"; import {CliArgsParser, getDefaultCliDescription} from "./src/CliArgsParser";
import {runWithFilteringAndConsoleAdapters} from "./src/Adapter"; import {runWithFilteringAndConsoleAdapters} from "./src/Adapter";
import {KotlinTestRunner} from "./src/KotlinTestRunner";
let kotlin_test
try {
kotlin_test = require('kotlin-test');
} catch {
}
if (kotlin_test) {
const parser = new CliArgsParser( const parser = new CliArgsParser(
getDefaultCliDescription(), getDefaultCliDescription(),
process.exit process.exit
); );
const untypedArgs = parser.parse(process.argv); const untypedArgs = parser.parse(process.argv);
const initialAdapter = kotlin_test.kotlin.test.detectAdapter_8be2vx$(); const adapterTransformer: (current: KotlinTestRunner) => KotlinTestRunner = current =>
kotlin_test.setAdapter(runWithFilteringAndConsoleAdapters(initialAdapter, untypedArgs)); runWithFilteringAndConsoleAdapters(current, untypedArgs);
(globalThis as any).kotlinTest = {
adapterTransformer: adapterTransformer
} }
+8
View File
@@ -3,6 +3,9 @@
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/ */
import {KotlinTestRunner} from "./src/KotlinTestRunner";
declare global {
interface Window { interface Window {
__karma__: { __karma__: {
config: { config: {
@@ -10,6 +13,11 @@ interface Window {
}, },
result: (result: BrowserResult) => void result: (result: BrowserResult) => void
} }
kotlinTest: {
adapterTransformer: (current: KotlinTestRunner) => KotlinTestRunner
}
}
} }
interface BrowserResult { interface BrowserResult {