Aurelia-cli,babel-plugin-istanbul,Karma代码覆盖报告总是返回100%

时间:2016-07-29 12:31:14

标签: karma-jasmine aurelia babel istanbul aurelia-cli

始终获得100%的覆盖率。

karma.conf.js文件与aurelia-cli生成的文件相同。

我想使用babel-plugin-istanbul是代码覆盖率报告。

package.json ~~>使用节点模块

"babel-plugin-istanbul": "^1.0.3",
"jasmine-core": "^2.4.1",
"karma": "^0.13.22",
"karma-chrome-launcher": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-babel-preprocessor": "^6.0.1",
"karma-coverage": "^1.1.0",
"karma-jasmine-html-reporter": "^0.2.0",
"karma-sinon": "^1.0.5",
"sinon": "^1.17.4"

karma.conf.js(与aurelia-cli生成的相同)

"use strict";
const path = require('path');
const project = require('./aurelia_project/aurelia.json');

let testSrc = [
  { pattern: project.unitTestRunner.source, included: false },
  'test/aurelia-karma.js'
];

let output = project.platform.output;
let appSrc = project.build.bundles.map(x => path.join(output, x.name));
let entryIndex = appSrc.indexOf(path.join(output, project.build.loader.configTarget));
let entryBundle = appSrc.splice(entryIndex, 1)[0];
let files = [entryBundle].concat(testSrc).concat(appSrc);

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: [project.testFramework.id, 'sinon'],
    files: files,
    exclude: [],
    preprocessors: {
      [project.unitTestRunner.source]: [project.transpiler.id]
    },
    'babelPreprocessor': { options: project.transpiler.options },
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};

cover.js文件

export function cover(done) {
  new Karma({
    configFile: __dirname + '/../../karma.conf.js',
    singleRun: true,
    reporters: ['coverage'],
    preprocessors: {
      'test/unit/**/*.js': ['babel'],
      'src/**/*.js': ['babel']
    },
    coverageReporter: {
      includeAllSources: true,
      reporters: [
        {type: 'html', dir: 'coverage'},
        {type: 'text'}
      ]
    }
  }, done).start();
}

由于

2 个答案:

答案 0 :(得分:1)

您无需在预处理器部分中包含测试。因此,删除第一行并仅保留源代码。覆盖范围仅涉及源代码而非测试方法。

preprocessors: {

      'src/**/*.js': ['babel']
    },

答案 1 :(得分:1)

我发现你至少缺少覆盖的预处理器:

preprocessors: {
  'test/unit/**/*.js': ['babel'],
  'src/**/*.js': ['babel', 'coverage']
},

尽管如此,即使有这样的问题,我也遇到了类似的问题。

主要问题是karma的默认aurelia-cli设置使用app bundle作为运行测试的源。使用此isparta方法并未使用此方法。

我的工作配置如下:

import gulp from 'gulp';
import {Server as Karma} from 'karma';
import {CLIOptions} from 'aurelia-cli';

let appBundle = "scripts/app-bundle.js";


export function cover(done) {
  new Karma({
    configFile: __dirname + '/../../karma.conf.js',
    singleRun: !CLIOptions.hasFlag('watch'),
    reporters: ['coverage'],
    logLevel: 'debug',
    preprocessors: {
      [project.unitTestRunner.source]: [project.transpiler.id],
      [appBundle]: ['coverage']
    },
    coverageReporter: {
      includeAllSources: true,
      instrumenters: {
        isparta: require('isparta')
      },
      instrumenter: {
        [appBundle]: 'isparta'
      },
      reporters: [
        { type: 'html', dir: '.reports/coverage' },
        { type: 'text' }
      ]
    }
  }, done).start();
}

export default cover;

有了这个,我可以用覆盖范围进行测试。唉,覆盖率报告基于单个app-bundle.js文件,该文件没有为您提供每个源文件的逐项报告。但它足以找到覆盖范围的主要差距,这是我主要用它来做的。