• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.outputFile.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
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.addAll([
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    def nodeProjDir = node.nodeProjectDir.getAsFile().get()
55    mochaChromeTestPage.text = """<!DOCTYPE html>
56        <html>
57        <head>
58            <title>Mocha Tests</title>
59            <meta charset="utf-8">
60            <link rel="stylesheet" href="$nodeProjDir/node_modules/mocha/mocha.css">
61        </head>
62        <body>
63        <div id="mocha"></div>
64        <script src="$nodeProjDir/node_modules/mocha/mocha.js"></script>
65        <script>mocha.setup('bdd');</script>
66        <script src="$nodeProjDir/node_modules/kotlin/kotlin.js"></script>
67        <script src="$nodeProjDir/node_modules/kotlin-test/kotlin-test.js"></script>
68        <script src="$compileJsLegacy.outputFile"></script>
69        <script src="$compileTestJsLegacy.outputFile"></script>
70        <script>mocha.run();</script>
71        </body>
72        </html>
73    """
74}
75
76task testMochaChrome(type: NodeTask, dependsOn: prepareMochaChrome) {
77    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'])
80}
81
82// todo: Commented out because mocha-headless-chrome does not work on TeamCity
83//jsTest.dependsOn testMochaChrome
84
85// -- Testing with Mocha under jsdom
86
87task installDependenciesMochaJsdom(type: NpmTask, dependsOn: [npmInstall]) {
88    args = ['install',
89            "mocha@$mocha_version",
90            "jsdom@$jsdom_version",
91            "jsdom-global@$jsdom_global_version",
92            "source-map-support@$source_map_support_version",
93            '--no-save']
94    if (project.hasProperty("teamcity")) args.addAll(["mocha-teamcity-reporter@$mocha_teamcity_reporter_version"])
95}
96
97task testMochaJsdom(type: NodeTask, dependsOn: [compileTestJsLegacy, installDependenciesMochaJsdom]) {
98    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'])
101}
102
103jsLegacyTestTask.dependsOn testMochaJsdom
104
105