1/* 2 * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 5// -- Testing with Mocha under Node 6 7task installDependenciesMochaNode(type: NpmTask, dependsOn: [npmInstall]) { 8 args = ['install', 9 "mocha@$mocha_version", 10 "source-map-support@$source_map_support_version", 11 '--no-save'] 12 if (project.hasProperty("teamcity")) args.addAll(["mocha-teamcity-reporter@$mocha_teamcity_reporter_version"]) 13} 14 15def compileJsLegacy = tasks.hasProperty("compileKotlinJsLegacy") 16 ? compileKotlinJsLegacy 17 : compileKotlinJs 18 19def compileTestJsLegacy = tasks.hasProperty("compileTestKotlinJsLegacy") 20 ? compileTestKotlinJsLegacy 21 : compileTestKotlinJs 22 23// todo: use atomicfu-transformed test files here (not critical) 24task testMochaNode(type: NodeTask, dependsOn: [compileTestJsLegacy, installDependenciesMochaNode]) { 25 script = file("${node.nodeProjectDir.getAsFile().get()}/node_modules/mocha/bin/mocha") 26 args = [compileTestJsLegacy.outputFileProperty.get().path, '--require', 'source-map-support/register'] 27 if (project.hasProperty("teamcity")) args.addAll(['--reporter', 'mocha-teamcity-reporter']) 28} 29 30def jsLegacyTestTask = project.tasks.findByName('jsLegacyTest') ? jsLegacyTest : jsTest 31 32// TODO 33//jsLegacyTestTask.dependsOn testMochaNode 34 35// -- Testing with Mocha under headless Chrome 36 37task installDependenciesMochaChrome(type: NpmTask, dependsOn: [npmInstall]) { 38 args = ['install', 39 "mocha@$mocha_version", 40 "mocha-headless-chrome@$mocha_headless_chrome_version", 41 "kotlin@$kotlin_version", 42 "kotlin-test@$kotlin_version", 43 '--no-save'] 44 if (project.hasProperty("teamcity")) args.addAll([ 45 "mocha-teamcity-reporter@$mocha_teamcity_reporter_version"]) 46} 47 48def mochaChromeTestPage = file("$buildDir/test-page.html") 49 50task prepareMochaChrome(dependsOn: [compileTestJsLegacy, installDependenciesMochaChrome]) { 51 outputs.file(mochaChromeTestPage) 52} 53 54prepareMochaChrome.doLast { 55 def nodeProjDir = node.nodeProjectDir.getAsFile().get() 56 mochaChromeTestPage.text = """<!DOCTYPE html> 57 <html> 58 <head> 59 <title>Mocha Tests</title> 60 <meta charset="utf-8"> 61 <link rel="stylesheet" href="$nodeProjDir/node_modules/mocha/mocha.css"> 62 </head> 63 <body> 64 <div id="mocha"></div> 65 <script src="$nodeProjDir/node_modules/mocha/mocha.js"></script> 66 <script>mocha.setup('bdd');</script> 67 <script src="$nodeProjDir/node_modules/kotlin/kotlin.js"></script> 68 <script src="$nodeProjDir/node_modules/kotlin-test/kotlin-test.js"></script> 69 <script src="${compileJsLegacy.outputFileProperty.get().path}"></script> 70 <script src="${compileTestJsLegacy.outputFileProperty.get().path}"></script> 71 <script>mocha.run();</script> 72 </body> 73 </html> 74 """ 75} 76 77task testMochaChrome(type: NodeTask, dependsOn: prepareMochaChrome) { 78 script = file("${node.nodeProjectDir.getAsFile().get()}/node_modules/mocha-headless-chrome/bin/start") 79 args = [compileTestJsLegacy.outputFileProperty.get().path, '--file', mochaChromeTestPage] 80 if (project.hasProperty("teamcity")) args.addAll(['--reporter', 'mocha-teamcity-reporter']) 81} 82 83// todo: Commented out because mocha-headless-chrome does not work on TeamCity 84//jsTest.dependsOn testMochaChrome 85 86// -- Testing with Mocha under jsdom 87 88task installDependenciesMochaJsdom(type: NpmTask, dependsOn: [npmInstall]) { 89 args = ['install', 90 "mocha@$mocha_version", 91 "jsdom@$jsdom_version", 92 "jsdom-global@$jsdom_global_version", 93 "source-map-support@$source_map_support_version", 94 '--no-save'] 95 if (project.hasProperty("teamcity")) args.addAll(["mocha-teamcity-reporter@$mocha_teamcity_reporter_version"]) 96} 97 98task testMochaJsdom(type: NodeTask, dependsOn: [compileTestJsLegacy, installDependenciesMochaJsdom]) { 99 script = file("${node.nodeProjectDir.getAsFile().get()}/node_modules/mocha/bin/mocha") 100 args = [compileTestJsLegacy.outputFileProperty.get().path, '--require', 'source-map-support/register', '--require', 'jsdom-global/register'] 101 if (project.hasProperty("teamcity")) args.addAll(['--reporter', 'mocha-teamcity-reporter']) 102} 103 104// TODO 105//jsLegacyTestTask.dependsOn testMochaJsdom 106