3

I can use jacoco on the JVM side, but what can I use on the JS side of the Multiplatform project?

1 Answer 1

3

For now, there are no integrated code coverage tools. But you can implement it manually using karma.config.d and https://karma-runner.github.io/0.8/config/coverage.html.

Note: you can do it only with browser target

How to setup: Necessary to add dependencies, ideally dev dependencies into test source set, but dev dependencies are possible only since 1.4-M3, so they can be replaced with usual npm

implementation(devNpm("istanbul-instrumenter-loader", "3.0.1"))
implementation(devNpm("karma-coverage-istanbul-reporter", "3.0.3"))

After that create js file in karma.config.d folder in the project folder

;(function(config) { // just IIFE to protect local variabled
    const path = require("path") // native Node.JS module
    config.reporters.push("coverage-istanbul")
    config.plugins.push("karma-coverage-istanbul-reporter")
    config.webpack.module.rules.push(
        {
            test: /\.js$/,
            use: {loader: 'istanbul-instrumenter-loader'},
            include: [path.resolve(__dirname, '../module-name/kotlin/')] // here is necessary to use module-name in `build/js/packages`
        }
    )
    config.coverageIstanbulReporter = {
        reports: ["html"]
    }
}(config));

It works with Kotlin code (but honestly report is arguable) but anyway, it provides both statistics for js and Kt files, but for js indicate 0%.

I created a feature request: https://youtrack.jetbrains.com/issue/KT-40460

Update: The HTML file with results is in build/js/packages/{$module-name}-test/coverage/index.html. You can run build or browserTest task.

NOTE: If you on Windows you have to change include: [path.resolve(__dirname, '../module-name/kotlin/')] to include: [path.resolve(__dirname, '..\\module-name\\kotlin\\')]

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much. I will try this today.
how do you run / generate the reports?
Updated the answer. Also, see a sample project - github.com/ilgonmic/kotlin-js-test-coverage

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.