让 Karma、6to5ify 和 Istanbul 发挥作用

Getting Karma, 6to5ify and Istanbul to play ball

我有 Browserify、6to5ify 和 Karma 可以很好地发挥作用,成功地 运行调整了我的规格。但是,当我添加代码覆盖率时,事情就变糟了。我尝试了几种方法:

现在我的 karma.conf.js 看起来像这样:

module.exports = function(karma){
  karma.set({
    frameworks: ["browserify", "mocha", "chai-sinon"],
    browserify: {
      debug: true,
      extensions: [".js", ".hbs"],
      transform: ["6to5ify", "hbsfy"]
    },
    reporters: ["dots", "osx", "junit", "coverage"],
    coverageReporter: {
      type: "text"
    },
    junitReporter: {
      outputFile: "spec/reports/test-results.xml"
    },
    preprocessors: {
      "src/javascript/**/*": ["coverage"],
      "spec/**/*": ["browserify"]
    },
    browsers: ["PhantomJS"],
    files: ["spec/unit/**/*Spec.js"],
    logLevel: "LOG_DEBUG",
    autoWatch: true
  });
};

我有点不知道如何让这一切一起工作。我尝试关注 these instructions,但没有成功,因为它没有关注我在 package.json 中的 browser 节点。任何帮助将不胜感激。

所以,显然我需要 browserify-istanbul,我需要 browserify configure 钩子,就像这样:

var to5ify = require('6to5ify');
var hbsfy = require('hbsfy');
var cover = require('browserify-istanbul');

var coverOptions = {
  ignore: ['**/*Spec.js', '**/lib/*.js', '**/fixtures/*.hbs'],
  defaultIgnore: true
}

module.exports = function(karma){
  karma.set({
    frameworks: ["browserify", "mocha", "chai-sinon"],
    browserify: {
      debug: false,
      extensions: [".js", ".hbs"],
      configure: function(bundle){
        bundle.on('prebundle', function(){
          bundle
            .transform(to5ify)
            .transform(hbsfy)
            .transform(cover(coverOptions));
        });
      }
    },
    reporters: ["dots", "osx", "junit", "coverage"],
    coverageReporter: {
      type: "text"
    },
    junitReporter: {
      outputFile: "spec/reports/test-results.xml"
    },
    preprocessors: {
      "spec/**/*": ["browserify"]
    },
    browsers: ["PhantomJS"],
    files: ["spec/unit/**/*Spec.js"],
    logLevel: "LOG_DEBUG",
    autoWatch: true
  });
};