42

Is there a way to exclude files from code coverage report for the karma coverage runner https://github.com/karma-runner/karma-coverage ?

Subtubes
  • 15,851
  • 22
  • 70
  • 105

2 Answers2

50

You can use several techniques here: karma uses minimatch globs for file paths and use can take advantage of that to exclude some paths.

As first solution I'd say try to add only the paths of the file to preprocess with the coverage:

// karma.conf.js
module.exports = function(config) {
  config.set({
    files: [
      'src/**/*.js',
      'test/**/*.js'
    ],

    // coverage reporter generates the coverage
    reporters: ['progress', 'coverage'],

    preprocessors: {
      // source files, that you wanna generate coverage for
      // do not include tests or libraries
      // (these files will be instrumented by Istanbul)
      'src/**/*.js': ['coverage']
    },

    // optionally, configure the reporter
    coverageReporter: {
      type : 'html',
      dir : 'coverage/'
    }
  });
};

The one above is the default example in karma-coverage and it shows that only those files in the src folder will be preprocessed.

Another trick can be to use the ! operator to exclude specific paths:

preprocessors: {
  // source files, that you wanna generate coverage for
  // do not include tests or libraries
  'src/**/!(*spec|*mock).js': ['coverage']
},

The one above makes the coverage run only on those Javascript files that do not end with spec.js or mock.js. The same can be done for folders:

preprocessors: {
  // source files, that you wanna generate coverage for
  // do not include tests or libraries
  'src/**/!(spec|mock)/*.js': ['coverage']
},

Do not process any Javascript files in the spec or mock folder.

danwellman
  • 9,068
  • 8
  • 60
  • 88
MarcoL
  • 9,829
  • 3
  • 37
  • 50
  • 5
    Tip: this will exclude all spec and mock folders, if they are in the src/spec and src/mock folders, then use like this: `'src/!(spec|mock)/**/*.js': ['coverage']` – Thiago Festa Sep 09 '15 at 13:47
  • Are you allowed to add multiple lines? `'src/**/*.js': ['coverage'],` `'src/!(vendor)/*.js,*.js}': ['coverage']` . What I'm looking for is adding every .js file but excluding .js files that are part of vendor – jmoon90 Aug 20 '18 at 17:36
  • 1
    Sure, you can add as many lines you want, as long each key/path are not duplicate. – MarcoL Aug 20 '18 at 17:38
  • @MarcoL i'm still getting vendor files in my lcov.info I've tried the above and this as well `'src/{**/!(vendor)/*.js,*.js}': ['coverage']` – jmoon90 Aug 20 '18 at 18:16
5

If you're using karma-esm or @open-wc/testing-karma, which uses karma-esm, pass an Array of glob strings to esm.coverageExclude

const { createDefaultConfig } = require('@open-wc/testing-karma');

const merge = require('deepmerge');

/**
 * @param  {import('@types/karma').Config} config
 * @return  {import('@types/karma').Config}
 */
module.exports = config => {
  config.set(merge(createDefaultConfig(config), {
    files: [{ pattern: config.grep ? config.grep : 'src/**/*.test.js', type: 'module' }],
    esm: {
      nodeResolve: true,
      babel: true,
      coverageExclude: ['src/*.test.js'],
    },
  }));
  return config;
};
Benny Powers
  • 5,398
  • 4
  • 32
  • 55