我使用的是IntelliJ IDEA多平台项目,jsTest Gradle任务不检测任何测试。jvmTest测试运行没有问题。当我使用调试输出运行jsTest任务时,我可以看到任务运行并立即完成。
Gradle版本4.10.1。科特林版本1.3.0-eap。
如何纠正Gradle配置,或者我可以运行什么命令,以便实际检测到测试,并且(如所写)失败?
build.gradle:
plugins {
id 'kotlin-multiplatform' version '1.3.0-rc-131'
}
repositories {
maven { url 'http://dl.bintray.com/kotlin/kotlin-eap' }
mavenCentral()
}
kotlin {
targets {
fromPreset(presets.jvm, 'jvm')
fromPreset(presets.js, 'js')
}
sourceSets {
commonMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-common'
}
}
commonTest {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-test-common'
implementation 'org.jetbrains.kotlin:kotlin-test-annotations-common'
}
}
jvmMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
}
}
jvmTest {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-test'
implementation 'org.jetbrains.kotlin:kotlin-test-junit'
}
}
jsMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-js'
}
}
jsTest {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-test-js'
}
}
}
}
test-project_test.js:
if (typeof kotlin === 'undefined') {
throw new Error("Error loading module 'test-project_test'. Its dependency 'kotlin' was not found. Please, check whether 'kotlin' is loaded prior to 'test-project_test'.");
}
if (typeof this['test-project'] === 'undefined') {
throw new Error("Error loading module 'test-project_test'. Its dependency 'test-project' was not found. Please, check whether 'test-project' is loaded prior to 'test-project_test'.");
}
if (typeof this['kotlin-test'] === 'undefined') {
throw new Error("Error loading module 'test-project_test'. Its dependency 'kotlin-test' was not found. Please, check whether 'kotlin-test' is loaded prior to 'test-project_test'.");
}
this['test-project_test'] = function (_, Kotlin, $module$test_project, $module$kotlin_test) {
'use strict';
var Sample = $module$test_project.sample.Sample;
var assertTrue = $module$kotlin_test.kotlin.test.assertTrue_ifx8ge$;
var Kind_CLASS = Kotlin.Kind.CLASS;
var hello = $module$test_project.sample.hello;
var contains = Kotlin.kotlin.text.contains_li3zpu$;
var test = $module$kotlin_test.kotlin.test.test;
var suite = $module$kotlin_test.kotlin.test.suite;
function SampleTests() {
}
SampleTests.prototype.testMe = function () {
assertTrue((new Sample()).checkMe() > 0);
};
SampleTests.$metadata$ = {
kind: Kind_CLASS,
simpleName: 'SampleTests',
interfaces: []
};
function SampleTestsJS() {
}
SampleTestsJS.prototype.testHello = function () {
assertTrue(contains(hello(), 'JSSDF'));
};
SampleTestsJS.$metadata$ = {
kind: Kind_CLASS,
simpleName: 'SampleTestsJS',
interfaces: []
};
var package$sample = _.sample || (_.sample = {});
package$sample.SampleTests = SampleTests;
package$sample.SampleTestsJS = SampleTestsJS;
suite('sample', false, function () {
suite('SampleTests', false, function () {
test('testMe', false, function () {
return (new SampleTests()).testMe();
});
});
suite('SampleTestsJS', false, function () {
test('testHello', false, function () {
return (new SampleTestsJS()).testHello();
});
});
});
Kotlin.defineModule('test-project_test', _);
return _;
}(typeof this['test-project_test'] === 'undefined' ? {} : this['test-project_test'], kotlin, this['test-project'], this['kotlin-test']);
受kotlinx-io的多平台构建配置的启发,在不真正知道我在做什么的情况下,我设法将JavaScript测试配置为使用Mocha运行。
compileKotlinJs.configure {
kotlinOptions {
metaInfo = true
sourceMap = true
moduleKind = 'umd'
main = "noCall"
sourceMapEmbedSources = 'always'
}
}
compileTestKotlinJs.configure {
kotlinOptions {
metaInfo = true
sourceMap = true
moduleKind = 'umd'
main = "call"
sourceMapEmbedSources = 'always'
}
}
task copyJsDependencies(type: Copy, dependsOn: compileTestKotlinJs) {
from compileKotlinJs.destinationDir
into "${buildDir}/node_modules"
def configuration = configurations.jsTestRuntimeClasspath
from(files {
configuration.collect { File file ->
file.name.endsWith(".jar")
? zipTree(file.absolutePath).matching {
include '*.js'
include '*.js.map' }
: files()
}
}.builtBy(configuration))
}
node {
version = nodeVersion
download = true
}
task installMocha(type: NpmTask) {
args = ['install', 'mocha']
}
task runMocha(type: NodeTask, dependsOn: [installMocha, compileTestKotlinJs, copyJsDependencies]) {
script = file('node_modules/mocha/bin/mocha')
args = [compileTestKotlinJs.outputFile]
}
jsTest.dependsOn runMocha
正如Kotlin多平台教程所述
此时,创建了Kotlin/JS的测试任务,但默认情况下不运行测试;它们应该手动配置为使用JavaScript测试框架运行测试。
例如,您可以使用mocha框架来运行测试
下面是我的设置:
build.gradle:
plugins {
id 'kotlin-multiplatform' version '1.3.10' //I'm using the released version of plugin,
//but it seems that they have same API
id 'com.moowork.node' version '1.2.0' //plugin for installing node
//and running node and npm tasks
}
repositories {
mavenCentral()
}
group 'com.example'
version '0.0.1'
apply plugin: 'maven-publish'
final kotlinRuntimeVersion = '1.3.10'
final nodeVersion = '11.2.0'
final nodeWorkingDir = project.buildDir
final nodeModules = "$nodeWorkingDir/node_modules"
final mochaVersion = '5.2.0'
final pathSeparator = System.properties["path.separator"]
kotlin {
targets {
fromPreset(presets.jvm, 'jvm')
fromPreset(presets.js, 'js') {
[compileKotlinJs, compileTestKotlinJs].each { configuration ->
configuration.kotlinOptions {
moduleKind = 'umd'
}
}
}
}
sourceSets {
commonMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-common'
}
}
commonTest {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-test-common'
implementation 'org.jetbrains.kotlin:kotlin-test-annotations-common'
}
}
jvmMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
}
}
jvmTest {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-test'
implementation 'org.jetbrains.kotlin:kotlin-test-junit'
}
}
jsMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-js'
}
}
jsTest {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-test-js'
}
}
}
}
//Workaround to copy kotlin libraries so they are visible during testing
def jsLibDir = "$compileKotlinJs.destinationDir/lib"
def jsTestLibDir = "$compileTestKotlinJs.destinationDir/lib"
configurations {
jsLibs
jsTestLibs
}
dependencies {
jsLibs "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlinRuntimeVersion"
jsTestLibs "org.jetbrains.kotlin:kotlin-test-js:$kotlinRuntimeVersion"
}
task copyJsDependencies(type: Copy, dependsOn: compileKotlinJs) {
configurations.jsLibs.each {
from zipTree(it.absolutePath).matching { include '*.js'}
}
into jsLibDir
}
jsMainClasses.dependsOn copyJsDependencies
task copyJsTestDependencies(type: Copy) {
configurations.jsTestLibs.each {
from zipTree(it.absolutePath).matching { include '*.js'}
}
into jsTestLibDir
}
jsTestClasses.dependsOn copyJsTestDependencies
//Use mocha to run js tests
node {
version = nodeVersion
download = true
workDir = file("$project.buildDir/nodejs")
nodeModulesDir = file(nodeWorkingDir)
}
task installMocha(type: NpmTask, group: 'npm') {
outputs.dir "$nodeModules/mocha"
args = ['install', "mocha@$mochaVersion"]
}
task runMocha(type: NodeTask, dependsOn: [installMocha, jsMainClasses, jsTestClasses], group: 'npm') {
environment = [ "NODE_PATH": "$jsLibDir$pathSeparator$jsTestLibDir$pathSeparator$compileKotlinJs.destinationDir" ]
script = file("$nodeWorkingDir/node_modules/mocha/bin/mocha")
args = [compileTestKotlinJs.outputFile]
}
jsTest.dependsOn runMocha
settings.gradle:
pluginManagement {
resolutionStrategy {
eachPlugin {
if (requested.id.id == "kotlin-multiplatform") {
useModule("org.jetbrains.kotlin:kotlin-gradle-plugin:${requested.version}")
}
}
}
}
rootProject.name = 'test'
出于某些原因,禁用gradle的元数据功能对于节点插件正常工作非常重要。
使用此设置,您将运行jsTest
gradle任务的js测试(例如对于CI很重要),但它们不会像java测试那样出现在想法窗口中,并且您仍然无法调试它们。
为此,在IntelliJ IDEA中,您可以创建自定义mocha运行/调试配置(从主菜单运行|编辑配置),并将其配置为类似于<code>runMocha
IntelliJ IDEA 2018.2.3(Community Edition)中的Kotlin多平台模板在中依赖于JUnit 4.12,用于项目的JVM部分: 值得注意的是,我使用的是Gradle4.10(一些较旧的示例使用,它从Gradle4.6起就被弃用了)。关于如何设置的文档已经过时并且稀少: 如上所示,IntelliJ中构建的默认项目模板不使用JUnit 5。 JUnit团队提供了示例
我在设置一个活动时遇到了问题,该活动驻留在KMM项目中的共享代码中,并使用Jetpack Compose。当活动是在Android源代码(/app)中时,这个操作很好。但我想发布一个共享的(/common)库,而不需要任何应用程序。 null 进程:com.surrus.peopleinspace,pid:4067 java.lang.nosuchmethoderror:没有静态方法setcont
我创建了maven项目,它由junit和spock测试组成。两个测试的代码。 在我的本地机器中,当我运行mvn测试时,它会欺骗两个测试类。我在git仓库上部署了项目,并为maven项目配置了jenkins。我推这个项目的存储库和执行作业,但是詹金斯只检测JUnit测试的AppTest类。我已经改变了pom.xml并添加了文件regadring到。我的项目结构。 文件组织的内容。spockframe
根据前面的问题,我创建了一个静态编程语言Gradle项目。我向其中添加了两个源文件: 我还将目录src标记为源根,将测试标记为测试源根。 如果我在别处创建与这些源文件完全相同的目录结构,并围绕它创建一个非渐变IntelliJ项目,我就能够编译和运行这些代码,包括测试。 然而,在我的Gradle版本的项目中,我无法从IntelliJ IDEA构建测试。我发现以下错误: 奇怪的是,如果我在构建包装器后
我想使用kotlin/multiplatform实现一个项目,该项目由jvm上的后端和js中的web应用组成。结构如下: 应用程序使用的数据类属于共享项目,但是要使用jpa,我需要jvm注释。 一种解决方案是不使用kotlin数据类并在jvm中继承。我还尝试使用实验性的<code>@optionalExpection 当与一起使用时,它们需要非注释类型,而这不能用 让多平台注释继承多平台注释是不可
报告工作的groovy代码是: 这将如何翻译成Kotlin DSL?我尝试了许多变体,其中一些编译和运行,但不创建所需的输出。可运行的罐子。