Exclude files from coverage when using Karma and Istanbul

I use Karma to test my JavaScript and get coverage reports. I use the coverage report in Istanbul, which is the default. Here is my preprocessor parameter:

preprocessors: { 'framework/**/*.js':'coverage', 'framework/*.js':'coverage', '!framework/node/**/*.js':'coverage', '!framework/test/**/*.js':'coverage', 'framework-lib/**/*.js':'coverage', '!framework-lib/tool-data-api/tool-data-api.js':'coverage' } 

As you can see, I'm trying to use "!" like a negation command that usually works with Node. However, it does not work here, and none of my directories are excluded.

Is there a way to do what I'm trying to accomplish?

+7
javascript karma-runner istanbul minmatch
source share
4 answers

According to https://karma-runner.imtqy.com/0.12/config/configuration-file.html :

** / *. js: All files with the js extension in all subdirectories ** /! (jquery) .js: Same as previous but excludes "jquery.js"
** / (foo | bar) .js: in all subdirectories all files are "foo.js" or "bar.js"

So, based on this, I tried the following:

 preprocessors: { 'framework/**/!(node|test)/*.js': 'coverage', 'framework-lib/**/!(tool-data-api).js': 'coverage' } 

That seems to have accomplished what you are looking for.

As a note to others who come here to learn how to target all EXCEPT.spec.js files, try:

 '**/!(*spec).js' 

Which seems to work for me.

+7
source share

Karma uses the minimatch.js lib file for math files. Therefore, you need to rewrite the rules. For example, in order to exclude a node from the node folder, it must have

 preprocessors: { 'framework/*[!node]/*.js':'coverage', } 
+3
source share

I'm not sure if you use "istanbul cover ..." to run a coverage report, but if so, you can use the -x flag to exclude files / patterns. Entering text in the "Istanbul reference booklet" will show you usage, including this.

  -x <exclude-pattern> [-x <exclude-pattern>] one or more fileset patterns eg "**/vendor/**" 
+1
source share

Make sure that the directories / file you want to exclude is not loaded by any other inclusion. e.g. 'framework / /. js ':' coverage 'will load the files you are trying to exclude in'! framework / node / /. js': 'coverage'

0
source share

All Articles