Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Karma/Istanbul代码覆盖率找不到函数,始终返回100%_Javascript_Code Coverage_Karma Runner_Browserify_Istanbul - Fatal编程技术网

Javascript Karma/Istanbul代码覆盖率找不到函数,始终返回100%

Javascript Karma/Istanbul代码覆盖率找不到函数,始终返回100%,javascript,code-coverage,karma-runner,browserify,istanbul,Javascript,Code Coverage,Karma Runner,Browserify,Istanbul,我正在尝试为我的Karma测试添加代码覆盖率,但是,尽管它找到了我正在测试的正确JS文件,但没有找到这些文件中的函数 从我所读到的到目前为止,我相信这是因为在被传递到伊斯坦布尔做报道之前,文件没有被正确地浏览,但我承认我是新的,所以我希望得到一些建议 这是我的JS文件(common.JS): 以下是我编写的测试: it('Client - Should get correct AjaxBaseUrl with /', function () { var clientSettings =

我正在尝试为我的Karma测试添加代码覆盖率,但是,尽管它找到了我正在测试的正确JS文件,但没有找到这些文件中的函数

从我所读到的到目前为止,我相信这是因为在被传递到伊斯坦布尔做报道之前,文件没有被正确地浏览,但我承认我是新的,所以我希望得到一些建议

这是我的JS文件(common.JS):

以下是我编写的测试:

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');
});
我的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,

    })
}
这确实会生成一个报告,但这显示了100%,在common.js文件中找到的唯一一行是:

require("C:\\Source\\ProjectName\\client\\scripts\\common.js");
我试图将Browerify Istanbul添加到组合中,在Karma.conf的顶部添加了一个require,在browserify部分添加了一个额外的转换

bundle.transform(istanbul)
但是,这只会使我的测试失败,并抛出几个错误:

“未定义”不是对象(正在评估) “uuu cov_qlfhxemt7fatximx0_uvqq.b['1'][0]” getAjaxBaseUrl@C:/Users/CHARLE~1.WIC/AppData/Local/Temp/0d61da722d2838c9 600d83d1cbb4c0b8.浏览等级:43:1498 C:/Users/CHARLE~1.WIC/AppData/Local/Temp/0d61da722d2838c9600d83d1cbb4c0b 8.browserify:51742:1849

16 02 2016 09:14:08.515:错误[覆盖范围]:[类型错误:无法读取 未定义]类型的属性“star t”错误:无法读取属性 未定义的“开始” 在C:\Source\ProjectName\node\u modules\伊斯坦布尔\lib\o object utils.js:59:44 at Array.forEach(本机) 位于Object.addDerivedInfoForFile(C:\Source\ProjectName\node\u modules\伊斯坦布尔\lib\Object utils.js:58:37) 位于Object.Collector.filecoverage for(C:\Source\ProjectName\node\u modules\伊斯坦布尔\lib\Collector.js:94:15) 在C:\Source\ProjectName\node\u modules\伊斯坦布尔\lib\r eport\html.js:558:90 at Array.forEach(本机) 在HtmlReport.Report.mix.writeReport(C:\Source\ProjectName\node\u modules\伊斯坦布尔\lib\Report\html.js:557:27) 在writeReport(C:\Source\ProjectName\node\u modules\k arma coverage\lib\reporter.js:62:16) 在C:\Source\ProjectName\node\u modules\karma coverage\lib\reporter.js:288:11 在C:\Source\ProjectName\node\u modules\karma\lib\help er.js:82:7 在FSReqWrap.oncomplete(fs.js:82:15)


我是遗漏了什么,还是走错了方向?

我也有同样的问题。对我有效的方法是从预处理器部分删除“覆盖”,并使用browserify伊斯坦布尔。另外,您希望将browserify配置为忽略测试文件

因此,预处理器的外观应该类似(删除了“覆盖范围”):

您的browserify配置应该如下所示:

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

希望对我有所帮助解决方案是:

从预处理器索引到karma.conf的*.js文件的值数组中删除“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']    
},
browserify: {
    configure: function (bundle) {
        bundle.transform('reactify', { extensions: ['.jsx'] });
        bundle.transform(require('browserify-istanbul')({
          ignore: ['**/test_unit/**']
        }));
    }        
},