karma-runner / karma-coverage

A Karma plugin. Generate code coverage.
MIT License
768 stars 247 forks source link

Coverage is off (karma-coverage, browserify-istanbul) #361

Open Eccentris opened 6 years ago

Eccentris commented 6 years ago

I'm running vue unittests using karma, phantomjs, jasmine and vue/test-utils. The unittests run fine and as expected. However, the coverage seems to be off. I am for example triggering button clicks and expect the functions that are called from that button click to be covered, but they aren't. Instead really random stuff like which type a prop is (String, for example) gets covered. I'm not sure if this is an issue with karma-coverage or if I'm using it wrong somehow. All suggestions are appreciated :). My karma.conf.js looks like this:

module.exports = function(config) {

  var doVueify = require('vueify');
  var doBabelify = require('babelify');
  var doCover = require('browserify-istanbul');

  var coverOptions = {
    ignore: ['tests/my_project/**/*.js'],
    defaultIgnore: true
  }

  config.set({

    basePath: '',

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

    browserify: {
        debug: true,
        extensions: [".js", ".vue"],
        configure: function(bundle){
            bundle.on('prebundle', function(){
                bundle
                    .transform(doBabelify)
                    .transform(doVueify)
                    .transform(doCover(coverOptions));
            });
        }
    },

    files: [
        'node_modules/es6-promise/dist/es6-promise.js',
        'node_modules/whatwg-fetch/fetch.js',
        'tests/my_project/**/*.js'
    ],

    preprocessors: {
      'tests/my_project/**/*.js': ['browserify']
    },

    reporters: ['spec', 'coverage'],

    coverageReporter: {
        dir: 'reports/coverage/javascript',
        includeAllSources: true,
        reporters: [
            { type : 'cobertura', subdir: '.', file: 'cobertura.xml' },
            { type : 'lcov', subdir: '.' },
            { type: 'text-summary' }
        ]
    },

    junitReporter: {
        outputFile: 'reports/test-results.xml',
        useBrowserName: false
    },

    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: false, 
    browsers: ['PhantomJS'],    
    singleRun: false,    
    concurrency: Infinity
  })
}

And my package.json like this

  "devDependencies": {
    "@mapbox/stylelint-processor-arbitrary-tags": "^0.2.0",
    "@vue/test-utils": "^1.0.0-beta.13",
    "ajv": "^5.3.0",
    "autoprefixer": "^7.1.4",
    "babel-cli": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "babelify": "^8.0.0",
    "browserify-istanbul": "^3.0.1",
    "eslint": "^4.17.0",
    "eslint-plugin-vue": "4.3.0",
    "es6-promise": "^2.3.0",
    "grunt": "^1.0.2",
    "grunt-browserify": "^5.2.0",
    "grunt-contrib-clean": "^1.1.0",
    "grunt-contrib-copy": "^1.0.0",
    "grunt-contrib-less": "^1.4.1",
    "grunt-contrib-watch": "^1.0.0",
    "grunt-eslint": "^20.1.0",
    "grunt-postcss": "^0.9.0",
    "grunt-stylelint": "^0.9.0",
    "jasmine": "^2.5.3",
    "karma": "^2.0.0",
    "karma-browserify": "^5.3.0",
    "karma-cli": "^1.0.1",
    "karma-coverage": "^1.1.1",
    "karma-jasmine": "^1.1.1",
    "karma-junit-reporter": "^1.2.0",
    "karma-phantomjs-launcher": "^1.0.4",
    "karma-spec-reporter": "0.0.32",
    "karma-sourcemap-loader": "^0.3.7",
    "stylelint": "^8.2.0",
    "stylelint-config-standard": "^17.0.0",
    "vueify": "^9.4.1"
  },
  "dependencies": {
    "@fortawesome/fontawesome": "^1.1.5",
    "@fortawesome/fontawesome-free-solid": "^5.0.10",
    "@fortawesome/vue-fontawesome": "0.0.22",
    "core-js": "^2.5.3",
    "normalize.css": "^7.0.0",
    "pikaday": "^1.6.1",
    "v-tooltip": "^2.0.0-rc.32",
    "vue": "^2.5.13",
    "whatwg-fetch": "^2.0.4"
  },
  "browser": {
    "vue": "vue/dist/vue.common.js"
  },
  "scripts": {
    "test": "npm run test:browser",
    "test:browser": "karma start --single-run --browsers PhantomJS",
    "test:ci": "npm run test:browser -- --reporters spec,coverage,junit"
  }
rooby commented 3 years ago

I see similar things. It seems for me that if I run with source maps it's usually fine but when I run without source maps I get some pretty random stuff highlighted as covered or not covered. Although I'm using Angular.