Excluding files from coverage when using Karma and Istanbul
Asked Answered
A

5

8

I am using Karma to test my JavaScript and get coverage reports. I am using the Istanbul coverage report, which is the default. Here is my preprocessors 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 am trying to use the "!" as a negate command, which usually works with Node. However, it is not working here and none of my directories are being excluded.

Is there any way to do what I am trying to accomplish?

Arturo answered 19/3, 2014 at 21:31 Comment(2)
Just do not write the patterns/files you do not want to be covered. Or in case you need to preprocess them, just not write coverage as preprocessor in the list (for each file you can either specify a string or a list of strings).Dowser
If you want to use the ! to exclude paths it won't work: see this issue: github.com/karma-runner/karma/issues/440Dowser
S
7

According to https://karma-runner.github.io/0.12/config/configuration-file.html:

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

So, based on this, I tried the following:

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

Which seems to have accomplished what you are looking for.

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

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

Which seems to work for me.

Sasser answered 11/2, 2015 at 0:48 Comment(0)
C
3

In the Karma used minimatch.js lib for mathing files. So you need rewrite you rules. For instance to exclude folder node node it should has

preprocessors: {
        'framework/*[!node]/*.js':'coverage',            
    }
Cantabrigian answered 13/5, 2014 at 14:46 Comment(2)
minimatch.js uses extglob syntax which is the weirdest beast I've seen so far: mywiki.wooledge.org/glob#extglobScrawny
how would you ignore any node_modules folders?Twelvemo
F
1

I'm not sure if you are running "istanbul cover ..." to run your coverage report, but if you are, you can use the -x flag to exclude files/patterns. Typing in "istanbul help cover" will show you usage including this.

 -x <exclude-pattern> [-x <exclude-pattern>]
              one or more fileset patterns e.g. "**/vendor/**"
Fingerbreadth answered 1/5, 2014 at 15:15 Comment(0)
G
0

Make sure that the directories / file you want to exclude are not loaded by any other include. for example, 'framework//.js':'coverage' will load files you are trying to exclude in '!framework/node//.js':'coverage'

Girhiny answered 21/4, 2014 at 15:53 Comment(1)
So how would you get ALL files from a directory EXCEPT *spec.js files?Amerce
B
0

you can modify in angular.json under test->options

"codeCoverageExclude": ["src/testing/**/*"]
Bergeman answered 10/11, 2022 at 16:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.