简体   繁体   English

如何从代码覆盖率编号中排除browserify生成的代码?

[英]How can I exclude browserify generated code from code coverage numbers?

I use browserify to bundle all our angular js code into one file. 我使用browserify将所有的角度js代码捆绑到一个文件中。 We use karma + jasmine to unit test this one file, app.js. 我们使用业力+茉莉花对该应用程序app.js进行单元测试。 As part of the bundling that browserify does, it injects a single line of code at the beginning of the file: 作为browserify绑定的一部分,它将在文件开头插入一行代码:

(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){

I tried putting a /*instanbul ignore next*/ above that line but causes the whole file to be ignored. 我尝试在该行的上方放置/*instanbul ignore next*/ ,但导致整个文件被忽略。 this one line is killing my branch coverage numbers. 这一行正在杀死我的分支机构覆盖率数字。 is there anyway to ignore this generated code? 反正有没有忽略此生成的代码?

It is always preferred to write the unit test for each file,before the bundle process. 在捆绑过程之前,总是最好为每个文件编写单元测试。 If you have a number of files, will be difficult to mock the dependencies and keep track of them. 如果您有许多文件,将很难模拟依赖关系并跟踪它们。 And we have only a number of options available in istanbul. 而且在伊斯坦布尔,我们只有许多选择。 And what you want to do is to skip the function definition header. 您要做的就是跳过函数定义头。 There is no possible way to ignore that particular line only. 没有可能的方法仅忽略该特定行。 But you can solve this issue by separating the unit test's into different files. 但是您可以通过将单元测试分成不同的文件来解决此问题。 That is preferred and easy to test. 那是首选并且易于测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM