简体   繁体   English

Karma / Istanbul Code Coverage找不到功能并始终返回100%

[英]Karma/Istanbul Code Coverage does not find functions and always returns 100%

I am attempting to add Code Coverage for my Karma tests, however although it finds the correct JS files that I'm testing, it does not find the functions inside those files. 我正在尝试为我的Karma测试添加代码覆盖率,但是虽然它找到了我正在测试的正确的JS文件,但它找不到这些文件中的函数。

From what I have read so far I believe it to be to do with the files not being correctly browserified before being passed to istanbul to do the coverage, but admittedly I am new to this so I'm hoping for some suggestions. 从我到目前为止所读到的内容来看,我认为在传递给伊斯坦布尔进行报道之前,文件没有被正确浏览,但不可否认我是新手,所以我希望能提出一些建议。

Here is my JS file(common.js): 这是我的JS文件(common.js):

var applicationSettings = require('./settings');

var common = {
    getAjaxBaseUrl: function () {
        var strVirtualDirectory = applicationSettings.VirtualDirectory;
        if (strVirtualDirectory.length > 1) {
            if (!strVirtualDirectory.startsWith("/")) {
                strVirtualDirectory = "/" + strVirtualDirectory;
            }
        }
    return strVirtualDirectory;
   }
}
module.exports = common;

And here are the tests I have written: 这是我写的测试:

it('Client - Should get correct AjaxBaseUrl with /', function () {
    var clientSettings = require('./../client/scripts/settings');
    var clientCommon = require('./../client/scripts/common');

    clientSettings.VirtualDirectory = '/VD';
    expect(clientCommon.getAjaxBaseUrl()).to.equal('/VD');

});

it('Client - Should get correct AjaxBaseUrl without /', function () {
    var clientSettings = require('./../client/scripts/settings');
    var clientCommon = require('./../client/scripts/common');

    clientSettings.VirtualDirectory = 'VD';
    expect(clientCommon.getAjaxBaseUrl()).to.equal('/VD');
});

My Karma.conf is below: 我的Karma.conf如下:

// Karma configuration
// Generated on Mon Jan 11 2016 09:43:00 GMT+0000 (GMT Standard Time)

module.exports = function (config) {
    config.set({

        // base path that will be used to resolve all patterns (eg. files, exclude)
        basePath: '',

        // frameworks to use
        // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
        frameworks: ['phantomjs-shim', 'browserify', 'mocha'],

        // list of files / patterns to load in the browser
        files: [
            'https://code.jquery.com/jquery-2.2.0.min.js',
            'http://cdn.kendostatic.com/2015.3.1111/js/kendo.all.min.js',
            'test_unit/*Spec.js',
            'client/scripts/*.js'
        ],

        // list of files to exclude
        exclude: [
        ],

        // preprocess matching files before serving them to the browser
        // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
        preprocessors: {
            'test_unit/*Spec.js': ['browserify'],
            'client/scripts/*.js': ['browserify', 'coverage']    
        },

        // test results reporter to use
        // possible values: 'dots', 'progress'
        // available reporters: https://npmjs.org/browse/keyword/karma-reporter
        reporters: ['progress', 'coverage', 'junit'],

        // Configure jUnit reporter
        junitReporter: {
            outputDir: '', // results will be saved as $outputDir/$browserName.xml 
            outputFile: undefined, // if included, results will be saved as $outputDir/$browserName/$outputFile 
            suite: '', // suite will become the package name attribute in xml testsuite element 
            useBrowserName: true // add browser name to report and classes names 
        },

        // Configure coverage reporter
        coverageReporter: {
            type: 'html',
            dir: 'test_coverage',
            subdir: '.',
            file: 'coverage.htm'
        },

        // web server port
        port: 9876,

        // enable / disable colors in the output (reporters and logs)
        colors: true,

        // level of logging
        // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
        logLevel: config.LOG_INFO,

        // enable / disable watching file and executing tests whenever any file changes
        autoWatch: false,

        browserify: {
            configure: function (bundle) {
                bundle.transform('reactify', { extensions: ['.jsx'] });
            }        
        },        

        // Continuous Integration mode
        // if true, Karma captures browsers, runs the tests and exits
        singleRun: true,

        // Concurrency level
        // how many browser should be started simultaneous
        concurrency: Infinity,

    })
}

This does produce a report, but this shows 100% and the only line found in the common.js file is: 这确实产生了一个报告,但这显示了100%,并且在common.js文件中找到的唯一行是:

require("C:\\Source\\ProjectName\\client\\scripts\\common.js");

I attempted to add Browerify-Istanbul into the mix, by adding a require for it at the top of the Karma.conf an additional transform in the browserify section 我尝试将Browerify-Istanbul添加到混合中,通过在Karma.conf的顶部添加一个需求,在browserify部分中添加一个额外的转换

bundle.transform(istanbul)

However this just makes my tests fail and throw several errors: 然而,这只会使我的测试失败并抛出几个错误:

undefined is not an object (evaluating '__cov_qQLFhXEMt7fatxiMx0_vQQ.b[' 1'][0]') getAjaxBaseUrl@C:/Users/CHARLE~1.WIC/AppData/Local/Temp/0d61da722d2838c9 600d83d1cbb4c0b8.browserify:43:1498 C:/Users/CHARLE~1.WIC/AppData/Local/Temp/0d61da722d2838c9600d83d1cbb4c0b 8.browserify:51742:1849 undefined不是一个对象(评估'__cov_qQLFhXEMt7fatxiMx0_vQQ.b ['1'] [0]')getAjaxBaseUrl @ C:/Users/CHARLE~1.WIC/AppData/Local/Temp/0d61da722d2838c9 600d83d1cbb4c0b8.browserify:43:1498 C: /Users/CHARLE~1.WIC/AppData/Local/Temp/0d61da722d2838c9600d83d1cbb4c0b 8.browserify:51742:1849

16 02 2016 09:14:08.515:ERROR [coverage]: [TypeError: Cannot read property 'star t' of undefined] TypeError: Cannot read property 'start' of undefined at C:\\Source\\ProjectName\\node_modules\\istanbul\\lib\\o bject-utils.js:59:44 at Array.forEach (native) at Object.addDerivedInfoForFile (C:\\Source\\ProjectName\\node_modules\\istanbul\\lib\\object-utils.js:58:37) at Object.Collector.fileCoverageFor (C:\\Source\\ProjectName\\node_modules\\istanbul\\lib\\collector.js:94:15) at C:\\Source\\ProjectName\\node_modules\\istanbul\\lib\\r eport\\html.js:558:90 at Array.forEach (native) at HtmlReport.Report.mix.writeReport (C:\\Source\\ProjectName\\node_modules\\istanbul\\lib\\report\\html.js:557:27) at writeReport (C:\\Source\\ProjectName\\node_modules\\k arma-coverage\\lib\\reporter.js:62:16) at C:\\Source\\ProjectName\\node_modules\\karma-coverage \\lib\\reporter.js:288:11 at C:\\Source\\ProjectName\\node_modules\\karma\\lib\\help er.js:82:7 at FSReqWrap.oncomplete (fs.js:82:15) 16 02 2016 09:14:08.515:错误[覆盖]:[TypeError:无法读取未定义的属性'star t'类型错误:无法在C:\\ Source \\ ProjectName \\ node_modules \\ istanbul \\ lib中读取未定义的属性'start' \\ o bject-utils.js:59:44位于Object.Collector的Object.addDerivedInfoForFile(C:\\ Source \\ ProjectName \\ node_modules \\ istanbul \\ lib \\ object-utils.js:58:37)的Array.forEach(native) .fileCoverageFor(C:\\ Source \\ ProjectName \\ node_modules \\ istanbul \\ lib \\ collector.js:94:15)位于C:\\ Source \\ ProjectName \\ node_modules \\ istanbul \\ lib \\ r \\ net \\ html.js:558:90 at Array .forEach(native)at writeReport(C:\\ Source \\ ProjectName \\ node_modules \\ k)中的HtmlReport.Report.mix.writeReport(C:\\ Source \\ ProjectName \\ node_modules \\ istanbul \\ lib \\ report \\ html.js:557:27) arma-coverage \\ lib \\ reporter.js:62:16)在C:\\ Source \\ ProjectName \\ node_modules \\ karma-coverage \\ lib \\ reporter.js:288:11 at C:\\ Source \\ ProjectName \\ node_modules \\ karma \\ lib \\ help er.js:82:7 at FSReqWrap.oncomplete(fs.js:82:15)

Am I missing something, or going about this the wrong way? 我错过了一些东西,还是以错误的方式解决这个问题?

I had the exact same issue. 我有完全相同的问题。 What worked for me was removing "coverage" from the preprocessors section AND using browserify-istanbul. 对我有用的是从预处理器部分删除“coverage”并使用browserify-istanbul。 Also, you want to configure browserify-istanbul to ignore your test files. 此外,您要配置browserify-istanbul以忽略您的测试文件。

So your preprocessors should look something like (removed 'coverage'): 所以你的预处理器应该看起来像(删除'覆盖'):

// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
    'test_unit/*Spec.js': ['browserify'],
    'client/scripts/*.js': ['browserify']    
},

And your browserify config should look something like: 您的browserify配置应该类似于:

browserify: {
    configure: function (bundle) {
        bundle.transform('reactify', { extensions: ['.jsx'] });
        bundle.transform(require('browserify-istanbul')({
          ignore: ['**/test_unit/**']
        }));
    }        
},        

Hope that helps 希望有所帮助

For me the solution was to: 对我来说,解决方案是:

delete 'coverage' in the array of values of *.js files from preprocessors index into karma.conf 从预处理器索引中将* .js文件的值数组中的'coverage'删除到karma.conf中

Hope that helps 希望有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM