Kotlin / kotlin-frontend-plugin

Gradle Kotlin (http://kotlinlang.org) plugin for frontend development
Apache License 2.0
563 stars 69 forks source link

Karma fails to find tests #119

Closed piacenti closed 6 years ago

piacenti commented 6 years ago

I have an multi-plataform application that runs without any issues. However, I'm trying to use the default karma integration to run unit tests but karma seems to be looking for the tests on the wrong places. It seems that it expects webpack to do something different than for the default application. Webpack just does the usual of creating app.bundle.js in the build/bundle folder.

I get this error when attempting to run tests:

23 10 2018 15:44:26.167:INFO [karma-server]: Karma v3.1.1 server started at http://0.0.0.0:9876/ 23 10 2018 15:44:26.168:INFO [launcher]: Launching browsers PhantomJS with concurrency unlimited 23 10 2018 15:44:26.195:INFO [launcher]: Starting browser PhantomJS 23 10 2018 15:44:26.482:INFO [PhantomJS 2.1.1 (Linux 0.0.0)]: Connected on socket ZXeA0OnDxQMvdZXmAAAA with id 9523427 23 10 2018 15:44:26.746:WARN [reporter]: SourceMap position not found for trace: { "message": "ReferenceError: Can't find variable: Set\nat http://localhost:9876/base/classes/kotlin/test/frontend_test.bundle.js?5e5fff17408a3041f3bf635b168fdb7afeb2ec18:21411", "str": "ReferenceError: Can't find variable: Set\nat http://localhost:9876/base/classes/kotlin/test/frontend_test.bundle.js?5e5fff17408a3041f3bf635b168fdb7afeb2ec18:21411" } 23 10 2018 15:44:26.746:WARN [reporter]: SourceMap position not found for trace: { "message": "ReferenceError: Can't find variable: Set\nat http://localhost:9876/base/classes/kotlin/test/frontend_test.bundle.js?5e5fff17408a3041f3bf635b168fdb7afeb2ec18:21411", "str": "ReferenceError: Can't find variable: Set\nat http://localhost:9876/base/classes/kotlin/test/frontend_test.bundle.js?5e5fff17408a3041f3bf635b168fdb7afeb2ec18:21411" } PhantomJS 2.1.1 (Linux 0.0.0) ERROR { "message": "ReferenceError: Can't find variable: Set\nat classes/kotlin/test/frontend_test.bundle.js:21411", "str": "ReferenceError: Can't find variable: Set\nat classes/kotlin/test/frontend_test.bundle.js:21411" }

Theses frontend_test.bundle.js files don't exist anywhere and I assume I would need additional webpack configurations to support it but the example projects don't have anything special done to support karma.

This is my configuration:

buildscript {
    repositories {
        mavenLocal()
        jcenter()
        maven { url "http://dl.bintray.com/kotlin/kotlin-eap" }
        maven { url "https://kotlin.bintray.com/kotlinx" }
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-frontend-plugin:0.0.37"
        classpath "org.jetbrains.kotlinx:kotlinx-gradle-serialization-plugin:$serialization_version"
    }
}

apply plugin: 'kotlin-platform-js'
apply plugin: 'org.jetbrains.kotlin.frontend'
apply plugin: 'kotlinx-serialization'
apply plugin: 'kotlin-dce-js'

sourceSets {
    main.kotlin.srcDirs = ['src']
    main.resources.srcDirs = ['resources']
    test.kotlin.srcDirs = ["test"]
}

repositories {
    mavenLocal()
    jcenter()
    maven { url "https://kotlin.bintray.com/kotlin-js-wrappers" }
    maven { url "https://kotlin.bintray.com/kotlinx" }
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
    compile "org.jetbrains:kotlin-react:16.5.2-pre.55-kotlin-$kotlin_version"
    compile "org.jetbrains:kotlin-react-dom:16.5.2-pre.55-kotlin-$kotlin_version"
    compile "org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:$serialization_version"
    expectedBy project(":common")
    testCompile "org.jetbrains.kotlin:kotlin-test-js:$kotlin_version"
}
kotlinFrontend {
    sourceMaps = true
    npm {
        dependency("react", "16.5.2")
        dependency("react-dom", "16.5.2")
        dependency("toastr", "2.1.4")
    }
    webpackBundle {
        bundleName = "app"
        sourceMapEnabled = true
    }
     karma {
        enableWebPack = true
    }
}

compileKotlin2Js {
    kotlinOptions.moduleKind = 'umd'
    kotlinOptions.sourceMap = true
}

compileTestKotlin2Js {
    kotlinOptions.moduleKind = "umd"
    kotlinOptions.sourceMap = true
}
piacenti commented 6 years ago

Ok so I commented out my custom webpack configuration //config.devtools="source-maps" and now I'm getting this error:

23 10 2018 16:31:51.799:INFO [karma-server]: Karma v3.1.1 server started at http://0.0.0.0:9876/ 23 10 2018 16:31:51.801:INFO [launcher]: Launching browsers PhantomJS with concurrency unlimited 23 10 2018 16:31:51.824:INFO [launcher]: Starting browser PhantomJS 23 10 2018 16:31:52.119:INFO [PhantomJS 2.1.1 (Linux 0.0.0)]: Connected on socket lHhTSSIUKt8Q1Kw5AAAA with id 21493831 PhantomJS 2.1.1 (Linux 0.0.0) ERROR { "message": "ReferenceError: Can't find variable: Set\nat undefined:79", "str": "ReferenceError: Can't find variable: Set\nat undefined:79" }

Leaving config.devtool= "eval-source-map" has the same result

piacenti commented 6 years ago

Changed my configuration to be more explicit

kotlinFrontend {
    sourceMaps = true
    npm {
        dependency("react", "16.5.2")
        dependency("react-dom", "16.5.2")
        dependency("toastr", "2.1.4")
        devDependency("karma")
        devDependency("qunit")
    }
    webpackBundle {
        bundleName = "app"
    }
    karma{
        preprocessors = ["webpack",'sourcemap']
        plugins = ["karma-webpack", "karma-phantomjs-launcher", "karma-junit-reporter", "karma-qunit", "karma-sourcemap-loader"]
    }
}

And ended up finding out that the problem was in my unit test trying to instantiate a react component. The basic assertTrue(true) works fine