Karma Code Coverage - Always 100%?
Asked Answered
R

1

14

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!

Reprography answered 30/6, 2015 at 11:44 Comment(1)
Caused by using both karma-browserify and karma-coverage preprocessors together. Take a look at https://mcmap.net/q/902604/-getting-karma-6to5ify-and-istanbul-to-play-ball for a working config that uses browserify and istanbul (what karma-coverage uses) together.Giselagiselbert
L
1

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.

Linzer answered 9/7, 2015 at 14:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.