| // Install Mocha via npm |
| task installDependenciesMochaNode(type: NpmTask, dependsOn: [npmInstall]) { |
| args = ['install', |
| "mocha@$mocha_version", |
| "source-map-support@$source_map_support_version", |
| '--no-save'] |
| if (project.hasProperty("teamcity")) args += [ |
| "mocha-teamcity-reporter@$mocha_teamcity_reporter_version"] |
| } |
| |
| // Find kotlin targets that are tests for JS platform and set them up with Mocha |
| kotlin.targets.matching { it.platformType.name == 'js' }.all { target-> |
| def testCompilation = target.compilations["test"] |
| def mainCompilation = target.compilations["main"] |
| |
| def testCompilationTask = tasks[testCompilation.compileKotlinTaskName] |
| def mainCompilationTask = tasks[mainCompilation.compileKotlinTaskName] |
| def targetName = target.name |
| |
| // Combine all required compilation results in the node_modules folder |
| // - main compilation result (compileKotlinJs) |
| // - all runtime dependencies of test sourceset |
| def dependenciesTask = task("${targetName}TestDependencies", |
| type: Sync, |
| group: 'build', |
| description: "Installs Node dependencies for 'test' compilation of target '${targetName}'", |
| dependsOn: [mainCompilationTask]) { |
| from mainCompilationTask.destinationDir |
| into "$node.nodeModulesDir/node_modules" |
| |
| def configuration = testCompilation.runtimeDependencyFiles |
| from(files { |
| configuration.collect { File file -> |
| file.name.endsWith(".jar") ? |
| zipTree(file.absolutePath).matching { |
| include '*.js' |
| include '*.js.map' |
| } : |
| files() |
| } |
| }.builtBy(configuration)) |
| } |
| |
| npmInstall.dependsOn dependenciesTask |
| |
| // Run mocha with node |
| def mochaTask = task("mocha${targetName.capitalize()}Test", |
| group: 'verification', |
| description: "Runs tests with Mocha for 'test' compilation of target '${targetName}'", |
| type: NodeTask, |
| dependsOn: [testCompilationTask, installDependenciesMochaNode]) { |
| script = file("$node.nodeModulesDir/node_modules/mocha/bin/mocha") |
| args = [testCompilationTask.outputFile, '--require', 'source-map-support/register'] |
| if (project.hasProperty("teamcity")) args += ['--reporter', 'mocha-teamcity-reporter'] |
| } |
| |
| tasks["${targetName}Test"].dependsOn mochaTask |
| } |