istanbul-instrumenter-loader:覆盖问题与来源不匹配

时间:2017-06-09 09:24:52

标签: webpack ecmascript-6 karma-runner istanbul

我正在使用istanbul-instrumenter-loader尝试为未转换的es6代码生成代码覆盖率报告,虽然一切运行正常,但生成的HTML输出中报告的问题似乎与实际来源不符

例如:

Incorrect coverage output

(如果图片被删除) const声明后面有3个“如果声明未涵盖”,即使在该行之后根本没有这样的声明或任何代码。有时“语句未涵盖”标记在字符串的中间,或对象声明中,或多个语句等中。

这是我的Karma配置文件:

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: ''
        }
    })
};

2 个答案:

答案 0 :(得分:2)

我遇到了类似的问题,并在istanbul-instrumenter-loader之前运行babel-loader解决了问题。

您需要替换:

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

使用:

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

答案 1 :(得分:0)

karma-coverage和karma-webpack似乎在一起工作

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

如果您配置正确,可以在https://github.com/webpack-contrib/istanbul-instrumenter-loader

获取更多信息

你可以为你的js文件/规范提供一个样本