How to get karma-coverage (istanbul) to check coverage of ALL source files?

Stop Slandering Monica Cellio picture Stop Slandering Monica Cellio · May 22, 2014 · Viewed 8.7k times · Source

The code structure

I have an app directory structure like

scripts/sequoia/                              
├── GraphToolbar.js                     
├── nodes                                     
│   ├── activityNode.js                       
│   └── annotationNode.js                     
├── OverviewCanvasControl.js                  
└── settings                                  
    ├── GraphControlSettingsFactory.js        
    └── SnapContextFactory.js                 

My test directory current looks thus

test/spec/                                        
├── GraphToolbarSpec.js                           
├── settings                                      
│   ├── graphControlSettingsFactorySpec.js        
│   └── snapContextFactorySpec.js                 
└── test-main.js

Note that I only have GraphToolbar and the settings/ files covered so far; there are no tests yet for OverviewCanvasControl.js or the nodes/ files.

The karma config

In my karma.conf.js (coverage refers to karma-coverage):

preprocessors: {                     
  'scripts/sequoia/**/*.js': ['coverage']
},                                   
reporters: ['progress','coverage'],

The problem

When I run karma, the coverage preprocessor & reporter run, but it only checks the files that already have specs written. I want to be reporting 0% coverage for OverviewCanvasControl.js and the nodes/ files that have no coverage. When a new file is created & karma is run, I want it to catch that that file has no Spec yet.

How can I get Karma to check all matching source files for coverage, not just those with specs already created?

Answer

cschuff picture cschuff · Jun 30, 2016

After searching for I while I found that it is quite easy. Add this to your karma.conf.js:

coverageReporter: {
    includeAllSources: true,
    reporters: [
        ...
    ]
}

BR Chris