从 Karma 覆盖率报告中排除 angular.js、angular-ui-router.js、angular-mocks.js 等导入文件

Posted

技术标签:

【中文标题】从 Karma 覆盖率报告中排除 angular.js、angular-ui-router.js、angular-mocks.js 等导入文件【英文标题】:Exclude import files like angular.js, angular-ui-router.js, angular-mocks.js etc from Karma coverage reports 【发布时间】:2017-07-10 14:14:23 【问题描述】:

我的覆盖率报告包括以下导入:

 node_modules\angular-mocks\             |    26.09 |    11.58 |     19.7 |    27.23 |                |
  angular-mocks.js                       |    26.09 |    11.58 |     19.7 |    27.23 |... 6,3107,3109 |
 node_modules\angular-resource\          |     7.77 |        0 |     3.13 |     8.11 |                |
  angular-resource.js                    |     6.81 |        0 |     3.13 |      7.1 |... 836,839,842 |
  index.js                               |      100 |      100 |      100 |      100 |                |
 node_modules\angular-scroll\            |     4.98 |     1.81 |        0 |     5.14 |                |
  angular-scroll.js                      |     4.09 |     1.81 |        0 |     4.22 |... 620,621,624 |
  index.js                               |      100 |      100 |      100 |      100 |                |
 node_modules\angular-ui-router\release\ |    11.21 |     0.41 |     0.35 |    12.77 |                |
  angular-ui-router.js                   |    11.21 |     0.41 |     0.35 |    12.77 |... 0,4602,4603 |
 node_modules\angular\                   |    17.58 |     5.43 |     13.7 |     17.9 |                |
  angular.js                             |    17.56 |     5.43 |     13.7 |    17.88 |... 30418,30423 |
  index.js                               |      100 |      100 |      100 |      100 |                |
 node_modules\webpack\buildin\           |      100 |       50 |       50 |      100 |                |
  module.js

我的 Karma 配置是:

// jshint ignore: start
// jscs:disable

// Global karma config
'use strict';


// START_CONFIT_GENERATED_CONTENT
// We want to re-use the loaders from the dev.webpack.config
var webpackConfig = require('./../webpack/webpack.test.config.js');
var preprocessorList = ['webpack', 'sourcemap'];

var karmaConfig =   
  plugins: [
    'karma-phantomjs-launcher',
    'karma-jasmine',
    'karma-junit-reporter',
    'karma-coverage',
    'karma-chrome-launcher',
    'karma-webpack',
    'karma-sourcemap-loader',
    'karma-threshold-reporter',
    'karma-htmlfile-reporter',
    'karma-babel-preprocessor'
  ],    
  files: [
    'node_modules/jquery/dist/jquery.js',
    'node_modules/phantomjs-polyfill/bind-polyfill.js',
    'config/testUnit/test.files.js'
  ],    
  preprocessors:         
    'config/testUnit/test.files.js': preprocessorList,
    'src/**/!*spec.js': ['coverage'],
     'src/app/**/*.js': ['coverage'],
  ,   
  reporters: ['progress', 'junit', 'coverage', 'threshold','html'],    
  coverageReporter: 
    dir: 'reports/coverage',
    includeAllSources:false,
    reporters: [
       type: 'cobertura', subdir: 'cobertura' ,
       type: 'lcovonly', subdir: 'lcov' ,
       type: 'html', subdir: 'html' ,
       type: 'json', subdir: 'json' ,
       type: 'text' 
    ]
  ,    
  junitReporter: 
    outputDir: 'reports/unit/',
    outputFile: 'test-results.xml'
  ,
  htmlReporter: 
    outputFile: 'reports/unit/Test-Results.html'
  ,     
  webpack: 
    // Load the source code using test.files.js
    entry: ,    
    module: 
      preLoaders: webpackConfig.module.preLoaders,
       postLoaders: [  //delays coverage til after tests are run, fixing transpiled source coverage error
            test: /\.js$/,
            exclude: /(test|node_modules|bower_components)\//,
            loader: 'istanbul-instrumenter'  ],
      loaders: webpackConfig.module.loaders
    ,
    plugins: webpackConfig.plugins.concat([new DefinePlugin(
      __karmaTestSpec: testFilesRegEx
    )]),
    resolve: webpackConfig.resolve,
    devtool: 'inline-source-map'      // Changed to allow the sourcemap loader to work: https://github.com/webpack/karma-webpack
  ,    
  webpackServer: 
    noInfo: true
  ,    
  singleRun: false,
  colors: true
;    
module.exports = karmaConfig;

我试过 exclude: /(test|node_modules|bower_components)// 但没有帮助。

如何从覆盖率报告中排除这些文件?

【问题讨论】:

您设置覆盖范围的配置是什么? 在问题中添加了业力配置 【参考方案1】:

我们可以使用“!”排除文件夹/文件符号。示例

 preprocessors: 
    'framework/**/!(node_modules|test|bower_components)/*.js': 'coverage'     
 

详情请参考-https://karma-runner.github.io/0.12/config/configuration-file.html

【讨论】:

删除/ 中的webpack postLoaders exclude 有助于解决问题 我正在研究 windows 环境。所以/ 是罪魁祸首

以上是关于从 Karma 覆盖率报告中排除 angular.js、angular-ui-router.js、angular-mocks.js 等导入文件的主要内容,如果未能解决你的问题,请参考以下文章

如何从代码覆盖率伊斯坦布尔记者中排除模拟文件

Karma-coverage 检查 JS 文件中的测试覆盖率,而不是 Angular 2 中的 TS

有没有办法从业力覆盖文本报告器结果中排除所有 100% 或阈值的文件?

Angular中伊斯坦布尔覆盖报告中的标记异常

Angular 单元测试中业力代码覆盖率报告中的 1x 3x 等是啥意思?

Angular CLI - 获取包含所有来源的覆盖率报告