我正在使用grunt-browserify+ browserify-istanbul+ grunt-contrib-jasmine+grunt-template-jasmine-istanbul作为解决方案。在使用browserify. _
先显示代码,我稍后再解释,
grunt.initConfig({
browserify: {
  // build specs using browserify
  specs: {
    src: ["spec/**/*Spec.js"],
    dest: "spec/build/specs.js",
    options: {
      debug: true
    }
  },
  // build source files using browserify and browserify-istanbul
  dev: {
    options: {
      debug: true,
      browserifyOptions: {
        standalone: 'abc'
      },
      transform: [['browserify-istanbul', {
        ignore: ['**/node_modules/**'], // ignore third party libs
        defaultIgnore: true
      }]]
    },
    src: ['abc.js'],
    dest: 'dist/abc.js'
  }
},
connect: {
  server: {
    options: {
      port: 7000
    }
  }
},
// test using jasmine, generate coverage report using istanbul
jasmine: {
  coverage: {
    src: ['dist/abc.js'],
    options: {
      junit: {
            path: 'bin/junit'
        },
      host: 'http://localhost:7000/',
      specs:  'spec/build/specs.js',
      keepRunner: true,
      summary: true,
      template: require('grunt-template-jasmine-istanbul'),
      templateOptions: {
        replace: false, // *** this option is very important
        coverage: 'bin/coverage/coverage.json',
        report: [
          {
          type: 'html',
          options: {
            dir: 'spec/coverage/html'
          }
        }]
      }
    }
  }      
}
  grunt.registerTask('specs', ['browserify:specs', 'browserify:dev', 'connect', 'jasmine']);
生成伊斯坦布尔覆盖率报告的步骤可以总结为三个:
- 仪器代码
 
- 运行测试
 
- 生成覆盖率报告
 
在我们的解决方案中,我们browerify-istanbul在第 1 步、第 2 步grunt-contrib-jasmine和runt-template-jasmine-istanbul第 3 步中使用。
browserify-istanbul将让您在 browserify 构建步骤中检测代码,这样,我们可以轻松忽略第三方库。但是grunt-template-jasmine-istanbul会再次检测代码。为避免这种情况,您可以在选项中设置replace为。false
参考:
- 伊斯坦布尔台阶
 
- 浏览伊斯坦布尔
 
- grunt-contrib-茉莉花
 
- grunt-template-jasmine-istanbul --
replace选项