7

标题真的说明了一切。尽管在互联网上搜索,我还没有找到一个解决这个问题的例子。

以下是一些未遂事件

这是我正在进行的代码https://github.com/wheresrhys/on-guard/tree/browserify(注意它是“browserify”分支 - Gruntfile.js 有点乱,但很快就会整理出来)。我的初步调查console.log表明,bundle.src.js页面中正在以某种方式加载,但是当测试运行(并通过!)时,其中的代码bundle.src.js没有运行,所以我觉得这可能是一个别名问题......仅限于 phantomjs,因为当我在 chrome 中打开 specrunner 时,代码正在运行。

4

1 回答 1

1

我正在使用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']);

生成伊斯坦布尔覆盖率报告的步骤可以总结为三个:

  1. 仪器代码
  2. 运行测试
  3. 生成覆盖率报告

在我们的解决方案中,我们browerify-istanbul在第 1 步、第 2 步grunt-contrib-jasminerunt-template-jasmine-istanbul第 3 步中使用。

browserify-istanbul将让您在 browserify 构建步骤中检测代码,这样,我们可以轻松忽略第三方库。但是grunt-template-jasmine-istanbul会再次检测代码。为避免这种情况,您可以在选项中设置replace为。false

参考:

  1. 伊斯坦布尔台阶
  2. 浏览伊斯坦布尔
  3. grunt-contrib-茉莉花
  4. grunt-template-jasmine-istanbul --replace选项
于 2016-12-27T06:23:06.177 回答