如何获得karma-coverage(伊斯坦布尔)来检查所有源文件的覆盖范围?

seq*_*ell 23 karma-runner istanbul

代码结构

我有一个app类似的目录结构

scripts/sequoia/                              
??? GraphToolbar.js                     
??? nodes                                     
?   ??? activityNode.js                       
?   ??? annotationNode.js                     
??? OverviewCanvasControl.js                  
??? settings                                  
    ??? GraphControlSettingsFactory.js        
    ??? SnapContextFactory.js                 
Run Code Online (Sandbox Code Playgroud)

我的test目录当前看起来如此

test/spec/                                        
??? GraphToolbarSpec.js                           
??? settings                                      
?   ??? graphControlSettingsFactorySpec.js        
?   ??? snapContextFactorySpec.js                 
??? test-main.js
Run Code Online (Sandbox Code Playgroud)

请注意,到目前为止我只有GraphToolbarsettings/文件一样; 有没有测试尚未进行OverviewCanvasControl.jsnodes/文件.

业力配置

在我karma.conf.js(coveragekarma-coverage):

preprocessors: {                     
  'scripts/sequoia/**/*.js': ['coverage']
},                                   
reporters: ['progress','coverage'],
Run Code Online (Sandbox Code Playgroud)

问题

当我运行业力时,覆盖预处理器和报告器运行,但它只检查已经编写规范的文件.我想报告0%的覆盖率OverviewCanvasControl.jsnodes/没有覆盖的文件.当创建一个新文件并运行karma时,我希望它能够捕获该文件还没有Spec.

如何让Karma检查所有匹配的源文件以获取覆盖范围,而不仅仅是那些已经创建了规范的文件?

seq*_*ell 2

我想出的解决方案:遍历源树并检查每个源 JS 文件是否存在规范文件。这假设每个源文件都有一个相应的Spec.js文件(驻留在相应的目录结构中)。

\n\n
    \n
  • app/scripts/moduleFoo.js\xe2\x86\x92test/spec/moduleFooSpec.js
  • \n
  • app/scripts/ns1/Utils/foo_bar.js\xe2\x86\x92test/spec/ns1/Utils/foo_barSpec.js
  • \n
\n\n

任务依赖于fs-tools npm 模块。

\n\n
var fsTools = require(\'fs-tools\');\n//... module.exports = function(grunt) { ... etc. (gruntfile setup)\n\n  grunt.registerTask(\'checkspecs\', \'ensure that all js files have Specs\', function(){\n    var done = this.async();\n    var srcPath = \'./\'+cfg.app+\'/scripts/\'; //Where are your scripts?\n    var testPath = \'./test/spec/\';          //Where are your specs?\n    var missingSpecs = [];\n    fsTools.walk(srcPath, \'.js$\', function(path,stats,callback){\n      var specPath = testPath + path.substring(path.indexOf(\'ptc\')+4);\n      //strip .js, add Spec.js\n      specPath = specPath.split(\'\').slice(0,-3).join(\'\') + \'Spec.js\';\n      if(!grunt.file.exists(specPath)){\n        missingSpecs.push(path);\n      }\n      callback();\n    }, function (err){\n      if(err){\n        grunt.log.error(err);\n        done(false);\n      }\n      if(missingSpecs.length > 0){\n        grunt.log.warn(\'`Spec.js` files are missing for the following files!!\');\n        missingSpecs.forEach(function(path){\n          grunt.log.warn(path);\n        });\n      }else{\n        grunt.log.ok(\'Spec files present for all source files\');\n      }\n      done(!err);                   //fail only if fsTools.walk throws\n      //done(!missingSpecs.length); //fail if any specs are "missing"\n    });\n  });\n
Run Code Online (Sandbox Code Playgroud)\n\n

为什么不将其打包为 grunt 任务并发布到 NPM?

\n\n

主要是因为它很大程度上依赖于项目中的特定路径和命名约定。它尚未排除目录/文件(这可能是必要的),如果您不这样做,*Spec.js它将无法工作。在我看来,获取此代码片段并对其进行自定义可能比将所有内容外部化并使其成为可配置任务更容易。以后可能会改变这一点。

\n