14

I'm using istanbul-instrumenter-loader to try to generate code coverage reports for my untranspiled es6 code, and while everything runs fine, the issues reported in the generated HTML output doesn't seem to line up with the actual source.

For example:

Incorrect coverage output

(in case the image is removed) A const declaration has 3 "if statement not covered" after it, even though there is no such statement or any code at all after that line. Sometimes "statement not covered" is marked in the middle of a string, or in an object declaration, or across multiple statements, etc etc.

Here's my Karma config file:

module.exports = function(config) {
    config.set({
        basePath: '../../',
        frameworks: [ 'qunit' ],

        files: [
            'test/index.js',

            // Session tickets
            { pattern: 'test/tickets/*.json', watched: true, included: false, served: true }

        ],

        preprocessors: {
            'test/index.js': 'webpack'
        },
        webpack: {
            module: {
                rules: [{
                    test: /\.js$/,
                    exclude: /node_modules/,
                    loaders: ['istanbul-instrumenter-loader', 'babel-loader']
                },
                {
                    test: /\.vue$/,
                    loaders: ['vue-loader']
                },
                {
                    test: /\.png$/,
                    loaders: ['url-loader']
                }]
            }
        },
        reporters: [ 'coverage-istanbul', 'progress' ],
        coverageIstanbulReporter: {
            type: 'html',
            dir: './coverage'
            fixWebpackSourcePaths: true
        },
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: [ 'Chrome' ],
        browserNoActivityTimeout: 30000,
        singleRun: true,
        concurrency: Infinity,
        client: {
            captureConsole: true
        },
        browserConsoleLogOptions: {
            terminal: true,
            level: ''
        }
    })
};
tacospice
  • 647
  • 5
  • 20

2 Answers2

4

I had a similar problem and solved it by running istanbul-instrumenter-loader before babel-loader.

You'd need to replace:

{
    test: /\.js$/,
    exclude: /node_modules/,
    loaders: ['istanbul-instrumenter-loader', 'babel-loader']
}

with:

{
    test: /\.js$/,
    exclude: /node_modules/,
    use: [
        {
            loader: 'babel-loader'
        },
        {
            loader: 'istanbul-instrumenter-loader',
            options: {
                esModules: true
            }
        }
    ]
}
Greg
  • 370
  • 5
  • 8
0

karma-coverage and karma-webpack seem to be working together

"karma": "^0.12.28",
"karma-coverage": "^0.2.7",
"karma-sourcemap-loader": "^0.3.2",
"karma-webpack": "^1.3.1",
"webpack": "^1.4.14"

Have you configured correctly , you can get more here https://github.com/webpack-contrib/istanbul-instrumenter-loader

and can u provide a a sample for your js file/spec

Monis Majeed
  • 1,358
  • 14
  • 21
  • I don't know if I've configured correctly, that's part of the question. I did post my configuration, though. – tacospice Aug 23 '17 at 14:13