Jurriaan Mous
10/28/2018, 3:39 PMtask installMocha(type: NpmTask) {
    args = ['install', 'mocha']
}
afterEvaluate {
    // Directory for all js test dependencies
    def testDir = "${buildDir}/jsTest"
    def projectName = name
    // Copy test files
    task copyJsTestOutput(type: Copy, dependsOn: [compileTestKotlinJs]) {
        from compileTestKotlinJs.outputFile.parentFile.path
        include "*.js"
        include "*.js.map"
        into testDir
    }
    // Copy all to be tested code and dependencies
    task copyJsMainOutputAndDependencies(type: Copy, dependsOn: compileKotlinJs) {
        from compileKotlinJs.destinationDir
        configurations.jsTestCompileClasspath.each {
            from(zipTree(it.absolutePath).matching { include '*.js.map'; include '*.js' })
        }
        into("${testDir}/node_modules")
    }
    // Use mocha as the test runner
    task runMocha(type: NodeTask, dependsOn: [copyJsMainOutputAndDependencies, copyJsTestOutput, installMocha]) {
        script = file('node_modules/mocha/bin/mocha')
        args = ["${testDir}/${projectName}_test.js"]
    }
    // Let mocha run when test is run in JS target
    jsTest.dependsOn runMocha
}