• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright 2016-2020 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 += ["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.nodeModulesDir/node_modules/mocha/bin/mocha")
26    args = [compileTestJsLegacy.outputFile, '--require', 'source-map-support/register']
27    if (project.hasProperty("teamcity")) args += ['--reporter', 'mocha-teamcity-reporter']
28}
29
30def jsLegacyTestTask = project.tasks.findByName('jsLegacyTest') ? jsLegacyTest : jsTest
31
32jsLegacyTestTask.dependsOn testMochaNode
33
34// -- Testing with Mocha under headless Chrome
35
36task installDependenciesMochaChrome(type: NpmTask, dependsOn: [npmInstall]) {
37    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']
43    if (project.hasProperty("teamcity")) args += [
44            "mocha-teamcity-reporter@$mocha_teamcity_reporter_version"]
45}
46
47def mochaChromeTestPage = file("$buildDir/test-page.html")
48
49task prepareMochaChrome(dependsOn: [compileTestJsLegacy, installDependenciesMochaChrome]) {
50    outputs.file(mochaChromeTestPage)
51}
52
53prepareMochaChrome.doLast {
54    mochaChromeTestPage.text = """<!DOCTYPE html>
55        <html>
56        <head>
57            <title>Mocha Tests</title>
58            <meta charset="utf-8">
59            <link rel="stylesheet" href="$node.nodeModulesDir/node_modules/mocha/mocha.css">
60        </head>
61        <body>
62        <div id="mocha"></div>
63        <script src="$node.nodeModulesDir/node_modules/mocha/mocha.js"></script>
64        <script>mocha.setup('bdd');</script>
65        <script src="$node.nodeModulesDir/node_modules/kotlin/kotlin.js"></script>
66        <script src="$node.nodeModulesDir/node_modules/kotlin-test/kotlin-test.js"></script>
67        <script src="$compileJsLegacy.outputFile"></script>
68        <script src="$compileTestJsLegacy.outputFile"></script>
69        <script>mocha.run();</script>
70        </body>
71        </html>
72    """
73}
74
75task testMochaChrome(type: NodeTask, dependsOn: prepareMochaChrome) {
76    script = file("$node.nodeModulesDir/node_modules/mocha-headless-chrome/bin/start")
77    args = [compileTestJsLegacy.outputFile, '--file', mochaChromeTestPage]
78    if (project.hasProperty("teamcity")) args += ['--reporter', 'mocha-teamcity-reporter']
79}
80
81// todo: Commented out because mocha-headless-chrome does not work on TeamCity
82//jsTest.dependsOn testMochaChrome
83
84// -- Testing with Mocha under jsdom
85
86task installDependenciesMochaJsdom(type: NpmTask, dependsOn: [npmInstall]) {
87    args = ['install',
88            "mocha@$mocha_version",
89            "jsdom@$jsdom_version",
90            "jsdom-global@$jsdom_global_version",
91            "source-map-support@$source_map_support_version",
92            '--no-save']
93    if (project.hasProperty("teamcity")) args += ["mocha-teamcity-reporter@$mocha_teamcity_reporter_version"]
94}
95
96task testMochaJsdom(type: NodeTask, dependsOn: [compileTestJsLegacy, installDependenciesMochaJsdom]) {
97    script = file("$node.nodeModulesDir/node_modules/mocha/bin/mocha")
98    args = [compileTestJsLegacy.outputFile, '--require', 'source-map-support/register', '--require', 'jsdom-global/register']
99    if (project.hasProperty("teamcity")) args += ['--reporter', 'mocha-teamcity-reporter']
100}
101
102jsLegacyTestTask.dependsOn testMochaJsdom
103
104