node.js - 为在 phantomjs 中的 browserify 包上运行(通过 grunt)的 Jasmine 测试生成 Istanbul 尔代码覆盖率报告

标签 node.js gruntjs phantomjs browserify istanbul

标题说明了一切。尽管搜索了互联网,但我还没有找到解决此问题的单个示例。

这里有一些有惊无险

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

最佳答案

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

生成 Istanbul 尔覆盖率报告的步骤可以归纳为三个:

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

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

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

引用:

  1. Istanbul steps
  2. broswerify-istanbul
  3. grunt-contrib-jasmine
  4. grunt-template-jasmine-istanbul -- 替换选项

关于node.js - 为在 phantomjs 中的 browserify 包上运行(通过 grunt)的 Jasmine 测试生成 Istanbul 尔代码覆盖率报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20647019/

相关文章:

javascript - JQuery 附加不加载切换按钮的 css

javascript - jit-咕噜声 : Plugin for the "clean" task not found?

gruntjs - grunt - 不给出任何输出

python - 使用 Pug (Jade) 和 Jinja2 诱人的语法

node.js - 在幻象中运行 'casperjs test'

javascript - 为什么我的 phantomjs 脚本输出没有截图(命令行 args 版本)

node.js - 在 AWS amplify 中重写和重定向

javascript - Node.js - 如何初始化/定义 req.body?

input - CasperJS 清除输入字段

mysql - 包含表时 Sequelize 返回 "Unknown column"