【问题标题】:I want to use more than one coverage report type inside karma.conf.js我想在 karma.conf.js 中使用多个覆盖率报告类型
【发布时间】:2017-10-03 20:56:00
【问题描述】:

我正在使用 Karma 编写 JS 单元测试用例,并使用 Istanbul 来获取覆盖率报告。 我的 karma.conf.js 文件如下 -

// karma.conf.js
module.exports = function(config) {
  config.set({
    files: [
      'test/**/*.js'
    ],

    // coverage reporter generates the coverage
    reporters: ['progress', 'coverage'],

    preprocessors: {
      // source files, that you wanna generate coverage for
      // do not include tests or libraries
      // (these files will be instrumented by Istanbul)
      'test/**/*.js': ['coverage']
    },

    // optionally, configure the reporter
    coverageReporter: {
      type : 'html',
      dir : 'coverage/'
    }
  });
};

在coverageReporter 中,我想使用类型“html”和“lcov”。为此,我将其更改如下--

报道记者:{ 类型:'html','lcov', 目录:'覆盖/' }

然后我执行了karma start karma.conf.js,但遇到了异常——

C:\abc\npm-1.4.9>karma start karma.conf.js
05 05 2017 16:57:00.369:ERROR [config]: Invalid config file!
  C:\abc\npm-1.4.9\karma.conf.js:45
      type : 'html','lcov',
                          ^
SyntaxError: Unexpected token ,
    at createScript (vm.js:53:10)
    at Object.runInThisContext (vm.js:95:10)
    at Module._compile (module.js:543:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)

非常感谢任何帮助。

【问题讨论】:

    标签: javascript karma-runner code-coverage istanbul


    【解决方案1】:

    您必须为 coverageReporter 属性使用具有此结构的配置:

    coverageReporter: {
      // specify a common output directory
      dir: 'coverage/',
      reporters: [
        // reporters not supporting the `file` property
        { type: 'html', subdir: '.' },
        { type: 'lcov', subdir: '.' },
    
      ]
    }
    

    这在 karma-coverage 的readme 中有描述。

    【讨论】:

    • 这行得通,但有一个副作用。我在 karma.conf.js(Chrome 和 PhantomJs)中提到了两个浏览器,因此通常会为每个浏览器分别生成报告,但上面的配置忽略了浏览器配置。有什么想法吗??? ——
    【解决方案2】:

    试试下面的例子:

    coverageReporter: {
        reporters: [
            {type: 'html', dir: 'html-coverage'},
            {type: 'lcov'}
        ]
    }
    

    【讨论】:

    • 哦,明白了..所以我总是需要在每个报告类型的花括号内添加类型和目录的组合。如果我们不提及任何目录,我注意到还有一件事,那么默认情况下它会创建“覆盖”目录并填充其中的所有内容。谢谢,@Vlad !!!
    猜你喜欢
    • 1970-01-01
    • 2016-08-04
    • 2013-05-19
    • 2020-05-05
    • 2016-07-12
    • 2013-08-27
    • 2020-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多