blob: 27d2e5b394b1ad744708d17d666593082e81cbba [file] [log] [blame]
Roman Elizarov1f74a2d2018-06-29 19:19:45 +03001/*
Aurimas Liutikas79e555e2021-05-17 17:41:41 +00002 * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
Roman Elizarov1f74a2d2018-06-29 19:19:45 +03003 */
4
Roman Elizarov5f5107e2018-03-05 12:51:23 +03005// -- Testing with Mocha under Node
6
Roman Elizarovf0fc7702018-03-06 12:45:08 +03007task installDependenciesMochaNode(type: NpmTask, dependsOn: [npmInstall]) {
Roman Elizarov6140ca32018-03-05 14:34:18 +03008 args = ['install',
9 "mocha@$mocha_version",
Roman Elizarov905a5122018-04-04 09:59:33 +030010 "source-map-support@$source_map_support_version",
Roman Elizarov6140ca32018-03-05 14:34:18 +030011 '--no-save']
Steve Elliottca095be2022-07-25 14:26:10 +000012 if (project.hasProperty("teamcity")) args.addAll(["mocha-teamcity-reporter@$mocha_teamcity_reporter_version"])
Roman Elizarov65eff0b2017-12-20 15:51:31 +030013}
14
Ilya Goncharov63910892020-05-28 17:58:48 +030015def compileJsLegacy = tasks.hasProperty("compileKotlinJsLegacy")
16 ? compileKotlinJsLegacy
17 : compileKotlinJs
18
19def compileTestJsLegacy = tasks.hasProperty("compileTestKotlinJsLegacy")
20 ? compileTestKotlinJsLegacy
21 : compileTestKotlinJs
22
Roman Elizarovb2479982018-10-08 17:53:42 +030023// todo: use atomicfu-transformed test files here (not critical)
Ilya Goncharov63910892020-05-28 17:58:48 +030024task testMochaNode(type: NodeTask, dependsOn: [compileTestJsLegacy, installDependenciesMochaNode]) {
Steve Elliottca095be2022-07-25 14:26:10 +000025 script = file("${node.nodeProjectDir.getAsFile().get()}/node_modules/mocha/bin/mocha")
26 args = [compileTestJsLegacy.outputFile.path, '--require', 'source-map-support/register']
27 if (project.hasProperty("teamcity")) args.addAll(['--reporter', 'mocha-teamcity-reporter'])
Roman Elizarov65eff0b2017-12-20 15:51:31 +030028}
29
Ilya Goncharov63910892020-05-28 17:58:48 +030030def jsLegacyTestTask = project.tasks.findByName('jsLegacyTest') ? jsLegacyTest : jsTest
Svyatoslav Kuzmichfa97af22020-03-11 18:56:26 +030031
Ilya Goncharov63910892020-05-28 17:58:48 +030032jsLegacyTestTask.dependsOn testMochaNode
Roman Elizarov5f5107e2018-03-05 12:51:23 +030033
Roman Elizarov5f5107e2018-03-05 12:51:23 +030034// -- Testing with Mocha under headless Chrome
35
Roman Elizarovf0fc7702018-03-06 12:45:08 +030036task installDependenciesMochaChrome(type: NpmTask, dependsOn: [npmInstall]) {
Roman Elizarov6140ca32018-03-05 14:34:18 +030037 args = ['install',
38 "mocha@$mocha_version",
39 "mocha-headless-chrome@$mocha_headless_chrome_version",
40 "kotlin@$kotlin_version",
41 "kotlin-test@$kotlin_version",
42 '--no-save']
Steve Elliottca095be2022-07-25 14:26:10 +000043 if (project.hasProperty("teamcity")) args.addAll([
44 "mocha-teamcity-reporter@$mocha_teamcity_reporter_version"])
Roman Elizarov5f5107e2018-03-05 12:51:23 +030045}
46
Roman Elizarovf0fc7702018-03-06 12:45:08 +030047def mochaChromeTestPage = file("$buildDir/test-page.html")
48
Ilya Goncharov63910892020-05-28 17:58:48 +030049task prepareMochaChrome(dependsOn: [compileTestJsLegacy, installDependenciesMochaChrome]) {
Roman Elizarovf0fc7702018-03-06 12:45:08 +030050 outputs.file(mochaChromeTestPage)
51}
Roman Elizarov5f5107e2018-03-05 12:51:23 +030052
53prepareMochaChrome.doLast {
Steve Elliottca095be2022-07-25 14:26:10 +000054 def nodeProjDir = node.nodeProjectDir.getAsFile().get()
Roman Elizarovf0fc7702018-03-06 12:45:08 +030055 mochaChromeTestPage.text = """<!DOCTYPE html>
56 <html>
57 <head>
58 <title>Mocha Tests</title>
59 <meta charset="utf-8">
Steve Elliottca095be2022-07-25 14:26:10 +000060 <link rel="stylesheet" href="$nodeProjDir/node_modules/mocha/mocha.css">
Roman Elizarovf0fc7702018-03-06 12:45:08 +030061 </head>
62 <body>
63 <div id="mocha"></div>
Steve Elliottca095be2022-07-25 14:26:10 +000064 <script src="$nodeProjDir/node_modules/mocha/mocha.js"></script>
Roman Elizarovf0fc7702018-03-06 12:45:08 +030065 <script>mocha.setup('bdd');</script>
Steve Elliottca095be2022-07-25 14:26:10 +000066 <script src="$nodeProjDir/node_modules/kotlin/kotlin.js"></script>
67 <script src="$nodeProjDir/node_modules/kotlin-test/kotlin-test.js"></script>
Ilya Goncharov63910892020-05-28 17:58:48 +030068 <script src="$compileJsLegacy.outputFile"></script>
69 <script src="$compileTestJsLegacy.outputFile"></script>
Roman Elizarovf0fc7702018-03-06 12:45:08 +030070 <script>mocha.run();</script>
71 </body>
72 </html>
73 """
Roman Elizarov5f5107e2018-03-05 12:51:23 +030074}
75
76task testMochaChrome(type: NodeTask, dependsOn: prepareMochaChrome) {
Steve Elliottca095be2022-07-25 14:26:10 +000077 script = file("${node.nodeProjectDir.getAsFile().get()}/node_modules/mocha-headless-chrome/bin/start")
78 args = [compileTestJsLegacy.outputFile.path, '--file', mochaChromeTestPage]
79 if (project.hasProperty("teamcity")) args.addAll(['--reporter', 'mocha-teamcity-reporter'])
Roman Elizarov5f5107e2018-03-05 12:51:23 +030080}
81
Roman Elizarovb1dad172018-03-05 17:47:10 +030082// todo: Commented out because mocha-headless-chrome does not work on TeamCity
Vsevolod Tolstopyatove50a0fa2019-01-28 11:34:24 +030083//jsTest.dependsOn testMochaChrome
Roman Elizarov5f5107e2018-03-05 12:51:23 +030084
anton.sukhonosenkobb0ad4a2018-11-06 19:46:00 +030085// -- Testing with Mocha under jsdom
86
87task installDependenciesMochaJsdom(type: NpmTask, dependsOn: [npmInstall]) {
88 args = ['install',
89 "mocha@$mocha_version",
Roman Elizarov738f5a22020-10-12 19:03:46 +030090 "jsdom@$jsdom_version",
91 "jsdom-global@$jsdom_global_version",
anton.sukhonosenkobb0ad4a2018-11-06 19:46:00 +030092 "source-map-support@$source_map_support_version",
93 '--no-save']
Steve Elliottca095be2022-07-25 14:26:10 +000094 if (project.hasProperty("teamcity")) args.addAll(["mocha-teamcity-reporter@$mocha_teamcity_reporter_version"])
anton.sukhonosenkobb0ad4a2018-11-06 19:46:00 +030095}
96
Ilya Goncharov63910892020-05-28 17:58:48 +030097task testMochaJsdom(type: NodeTask, dependsOn: [compileTestJsLegacy, installDependenciesMochaJsdom]) {
Steve Elliottca095be2022-07-25 14:26:10 +000098 script = file("${node.nodeProjectDir.getAsFile().get()}/node_modules/mocha/bin/mocha")
99 args = [compileTestJsLegacy.outputFile.path, '--require', 'source-map-support/register', '--require', 'jsdom-global/register']
100 if (project.hasProperty("teamcity")) args.addAll(['--reporter', 'mocha-teamcity-reporter'])
anton.sukhonosenkobb0ad4a2018-11-06 19:46:00 +0300101}
102
Ilya Goncharov63910892020-05-28 17:58:48 +0300103jsLegacyTestTask.dependsOn testMochaJsdom
anton.sukhonosenkobb0ad4a2018-11-06 19:46:00 +0300104