How do I exclude third party imports like jquery from code coverage in karma using isparta?

1k views Asked by At

I'm using karma, webpack, and jasmine to test my ES6 code, with istanbul and isparta for code coverage. My tests all seem to pass, but the coverage is really low because I had to import jquery and jquery-resizable-dom, which gets included in the coverage as untested code.

I have a lot of vanilla js code (~200 lines) but then added something like this:

import $ from 'jquery';
require('imports?jQuery=jquery!jquery-resizable-dom');

const makeImagesResizable = () => {
  $('.img').resizable({
    handleSelector: '> .glyphicon-resize-full'
  });
};

export { makeImagesResizable };

And in my jasmine test, I imported makeImagesResizable to test it. When I run the test, I get something like 27% code coverage because it includes jquery code in the coverage. Removing the whole code block above (the jquery import and all the code that uses jquery) bumps the coverage to 94% which is closer to the actual coverage (the jquery code is pretty short).

Is there a way to exclude third party imports from the code coverage to reflect the numbers we actually get from the code?

Here is my karma config also:

const webpack = require('webpack');
const isparta = require('isparta');

module.exports = (config) => {
  config.set({
    frameworks: ['jasmine'],
    files: [
      './spec/*.spec.js',
    ],
    webpack: {
      module: {
        loaders: [
          { test: /\.js$/, loader: 'imports?define=>false!babel' },
        ],
      },
    },
    webpackMiddleware: {
      noInfo: true,
    },
    preprocessors: {
      './spec/*.spec.js': ['webpack', 'coverage'],
    },
    reporters: ['progress', 'coverage'],
    coverageReporter: {
      instrumenters: { isparta },
      reporters: [
        {
          type: 'html',
          dir: 'coverage',
        },
      ],
    },
    browsers: ['Chrome'],
    plugins: [
      'karma-webpack',
      'karma-jasmine',
      'karma-coverage',
      'karma-chrome-launcher',
    ],
  });
};

I only included test files since including the source files doesn't return any coverage at all for some reason.

1

There are 1 answers

1
suvartheec On BEST ANSWER

Quoting their guidebook from http://karma-runner.github.io/1.0/config/configuration-file.html you can use the exclude option to prevent them from effecting your results.