[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,11 +65,26 @@ internal fun adapter(): FrameworkAdapter {
return result
}
@JsName("kotlinTest")
external val kotlinTestNamespace: KotlinTestNamespace
internal fun detectAdapter() = when {
isQUnit() -> QUnitAdapter()
isJasmine() -> JasmineLikeAdapter()
else -> BareAdapter()
external interface KotlinTestNamespace {
val adapterTransformer: ((FrameworkAdapter) -> FrameworkAdapter)?
}
internal fun detectAdapter(): FrameworkAdapter {
val frameworkAdapter = when {
isQUnit() -> QUnitAdapter()
isJasmine() -> JasmineLikeAdapter()
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(
+19 -22
View File
@@ -5,28 +5,25 @@
import {CliArgsParser, getDefaultCliDescription} from "./src/CliArgsParser";
import {runWithFilteringAndConsoleAdapters} from "./src/Adapter";
import {KotlinTestRunner} from "./src/KotlinTestRunner";
let kotlin_test
try {
kotlin_test = require('kotlin-test');
} catch {
const parser = new CliArgsParser(
getDefaultCliDescription(),
(exitCode) => {
throw new Error(`Exit with ${exitCode}`)
}
);
const untypedArgs = parser.parse(window.__karma__.config.args);
const adapterTransformer: (current: KotlinTestRunner) => KotlinTestRunner = current =>
runWithFilteringAndConsoleAdapters(current, untypedArgs);
window.kotlinTest = {
adapterTransformer: adapterTransformer
}
if (kotlin_test) {
const parser = new CliArgsParser(
getDefaultCliDescription(),
(exitCode) => {
throw new Error(`Exit with ${exitCode}`)
}
);
const untypedArgs = parser.parse(window.__karma__.config.args);
const initialAdapter = kotlin_test.kotlin.test.detectAdapter_8be2vx$();
kotlin_test.setAdapter(runWithFilteringAndConsoleAdapters(initialAdapter, untypedArgs));
const resultFun = window.__karma__.result;
window.__karma__.result = function (result) {
console.log(`--END_KOTLIN_TEST--\n${JSON.stringify(result)}`);
resultFun(result)
};
}
const resultFun = window.__karma__.result;
window.__karma__.result = function (result) {
console.log(`--END_KOTLIN_TEST--\n${JSON.stringify(result)}`);
resultFun(result)
};
+10 -13
View File
@@ -1,19 +1,16 @@
import {CliArgsParser, getDefaultCliDescription} from "./src/CliArgsParser";
import {runWithFilteringAndConsoleAdapters} from "./src/Adapter";
import {KotlinTestRunner} from "./src/KotlinTestRunner";
let kotlin_test
try {
kotlin_test = require('kotlin-test');
} catch {
}
const parser = new CliArgsParser(
getDefaultCliDescription(),
process.exit
);
const untypedArgs = parser.parse(process.argv);
if (kotlin_test) {
const parser = new CliArgsParser(
getDefaultCliDescription(),
process.exit
);
const untypedArgs = parser.parse(process.argv);
const adapterTransformer: (current: KotlinTestRunner) => KotlinTestRunner = current =>
runWithFilteringAndConsoleAdapters(current, untypedArgs);
const initialAdapter = kotlin_test.kotlin.test.detectAdapter_8be2vx$();
kotlin_test.setAdapter(runWithFilteringAndConsoleAdapters(initialAdapter, untypedArgs));
(globalThis as any).kotlinTest = {
adapterTransformer: adapterTransformer
}
+14 -6
View File
@@ -3,12 +3,20 @@
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
interface Window {
__karma__: {
config: {
args: string[]
},
result: (result: BrowserResult) => void
import {KotlinTestRunner} from "./src/KotlinTestRunner";
declare global {
interface Window {
__karma__: {
config: {
args: string[]
},
result: (result: BrowserResult) => void
}
kotlinTest: {
adapterTransformer: (current: KotlinTestRunner) => KotlinTestRunner
}
}
}