Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Karma Code Coverage - Always 100%?

Good Morning,

I am having a weird issue that I cannot seem to solve. I have my Karma tests written out and the execute correctly, but when I try to wire up the code coverage for Karma it just spits out 100% no matter what.

I looked at the other questions that were raised here and none of them seemed to solve my issue. Any help would be greatly appreciated.

Using:

"karma": "~0.12.37",
"karma-babel-preprocessor": "^5.2.1",
"karma-browserify": "^4.2.1",
"karma-coverage": "^0.4.2",
"karma-jasmine": "~0.3.5",
"karma-phantomjs-launcher": "^0.2.0",

Here is my karma.conf.js

module.exports = function (config) {
  config.set({
    basePath: '',

    frameworks: ['browserify', 'jasmine'],

    files: [
        'bower_components/jquery/dist/jquery.js',
        'bower_components/angular/angular.js',
        'bower_components/angular-animate/angular-animate.js',
        'bower_components/angular-cookies/angular-cookies.js',
        'bower_components/angular-mocks/angular-mocks.js',
        'bower_components/angular-resource/angular-resource.js',
        'bower_components/angular-sanitize/angular-sanitize.js',
        'bower_components/angular-touch/angular-touch.js',
        'bower_components/angular-ui-router/release/angular-ui-router.js'
        'src/*.html',
        'src/**/*.html',
        'src/app/index.js',
        'src/app/**/*.js'
    ],

    exclude: [],

    preprocessors: {
        'src/app/index.js': ['browserify', 'coverage'],
        'src/app/**/*.js': ['browserify', 'coverage']
    },

    browserify: {
        debug: true,
        transform: ['babelify', 'stringify']
    },

    reporters: ['progress', 'coverage'],

    port: 9876,

    colors: true,

    autoWatch: true,

    browsers: ['PhantomJS'],

    singleRun: false
  });
};

My file structure is:

src
  app
    login
      login.controller.js
      login.controller.spec.js
      login.html
    index.js
karma.conf.js

Thank you!

like image 847
Primm Avatar asked Jun 30 '15 11:06

Primm


People also ask

Can you get 100% code coverage?

A 100% code coverage does not mean that 100% of lines are covered, but that 100% of the code which must be tested is actually tested.

How much code coverage is ideal?

With that being said it is generally accepted that 80% coverage is a good goal to aim for. Trying to reach a higher coverage might turn out to be costly, while not necessary producing enough benefit. The first time you run your coverage tool you might find that you have a fairly low percentage of coverage.

What is code coverage?

Code coverage is a software testing metric that determines the number of lines of code that is successfully validated under a test procedure, which in turn, helps in analyzing how comprehensively a software is verified. Developing enterprise-grade software products is the ultimate goal of any software company.

What is increase code coverage?

#2 Increase Code Coverage Code coverage is determined by how many lines of code a component has and how many of these lines get executed in test cases.


1 Answers

Have you tried using browserify-istanbul transform?

module.exports = function(config) {
    config.set({
         // ...
         browserify: {
             transform: ['browserify-istanbul', ...]
         }
    });
};

You need to "instrument" your code to collect coverage metrics. So you should tell browserify to apply instrumentation before returning the module with require.

like image 146
dragn Avatar answered Jan 01 '23 20:01

dragn