Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I exclude files from karma code coverage report?

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

like image 540
Subtubes Avatar asked Mar 19 '15 01:03

Subtubes


People also ask

How do you exclude files from code coverage?

To exclude test code from the code coverage results and only include application code, add the ExcludeFromCodeCoverageAttribute attribute to your test class. To include assemblies that aren't part of your solution, obtain the . pdb files for these assemblies and copy them into the same folder as the assembly .

How do I generate a coverage report on karma?

To activate the coverage reporter add this to your configuration file. reporters = ['coverage']; This will create a coverage report for every browser that the tests are run in. In addition, it will create a JSON file that outputs the intermediate data.

What is karma coverage Istanbul reporter?

A karma reporter that uses the latest istanbul 1. x APIs (with full sourcemap support) to report coverage.


2 Answers

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.

like image 182
MarcoL Avatar answered Sep 27 '22 20:09

MarcoL


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; }; 
like image 44
Benny Powers Avatar answered Sep 27 '22 19:09

Benny Powers