proper pattern for preprocessor directory exclusion

2,763 views
Skip to first unread message

Ron Derksen

unread,
Aug 29, 2013, 5:16:09 AM8/29/13
to karma...@googlegroups.com
Hi, I've been trying for a few hours now to exclude my test dirs from the coverage preprocessor, but I can't seem to find the right pattern. I was wondering if you could help me out.

I have multiple modules, which follow the same basic directory structure:















I'm looking for a pattern that will exclude everything in the test directory, but will instrument all the other files in the module (so the javascript-files in de models and views directories should be instrumented as well).

I've tried the following patterns:
src/**/!(test)/**/*.js
src/**/!(test)/*.js
!**/test/**/*.js

When I use that last pattern in a simple file that loads minimatch and test various paths it works as expected. However, when I put that pattern in the karma configuration, I get no files instrumented at all. I've also tried to first include all files, and then use the exclusion pattern to exclude the test files, but that didn't work either.

I hope someone here can help me out, because this is getting really annoying.

Thanks,
Ron Derksen

Vojta Jína

unread,
Sep 2, 2013, 3:57:06 PM9/2/13
to karma...@googlegroups.com
Hi Ron,

the reason why the last pattern does not work is because Karma resolve all the patterns to the basePath. Here's an issue for fixing the patterns that start with "!":

I know the current behavior of preprocessor/minimatching is not intuitive, would like to make it better, but not sure how. Before, people had too many issues doing patterns like "*.js", which minimatch wouldn't match (because internally all the paths are absolutely) and so we changed that to resolve these patterns to basePath. Any suggestions are welcome.

Some suggestions for your case:
- if you used a convention of naming test files x.spec.js, you could use 'src/**/!(*.spec).js' to match all non-test js files
- 'src/*/!(test)/**/*.js' should work (assuming the "test" directory is always the second level in "src"
- you can use multiple patterns, eg. {'src/*/!(test)/*.js': ['coverage'], 'src/!(test)/*.js': ['coverage']}

Hope this helps,
V.



--
You received this message because you are subscribed to the Google Groups "karma-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to karma-users...@googlegroups.com.
To post to this group, send email to karma...@googlegroups.com.
Visit this group at http://groups.google.com/group/karma-users.
For more options, visit https://groups.google.com/groups/opt_out.

João Morais

unread,
Apr 16, 2014, 7:12:14 PM4/16/14
to karma...@googlegroups.com
Hi guys, 

I have a problem similar to what is described above, though my folder structure is a bit different.
Grabbing the Preprocessor example, what I have is this:

files = [
  'lib/*.js',
  'test/lib/*.js'
];

So my guess, based on the comments above, is that the preprocessors should be declared with an exclusion (in order to ignore the test folder) something like this:

preprocessors = {
  '!(test)+(lib)/*.js': ['coverage']
};

Though it doesn't seem to work.. 
Am I missing something?

Any help is deeply appreciated, thanks in advance.

Vojta Jína

unread,
Apr 21, 2014, 6:14:54 PM4/21/14
to karma...@googlegroups.com
Hi João

did you try just:

preprocessors = {
  'lib/*.js': ['coverage']
};

That should work...


jcsm...@gmail.com

unread,
Apr 23, 2014, 1:54:29 PM4/23/14
to karma...@googlegroups.com
Hi Vojta,

Yes I did and it actually works.
The problem I had was on the files definition but at that time I was (wrongly) suspecting that the test folder was being included in the code coverage due to the folder structure thus the need to add an exclusion on the pre-processors.

I also set up an example just to make sure that the problem wasn't related the folder structure - it might get handy if anyone gets a similar doubt.

Thanks for your help.
Reply all
Reply to author
Forward
0 new messages