Istanbul code coverage force instrumentation of all files
The Istambul code coverage tool is really nice but, it shows you the coverage of your specs instead of your code.
I want to be able to say that my code have 100% coverage, instead of saying that i have 100% coverage from my specs.
The Istanbul instrument all files that required by your specs and generate code coverage reports, with pretty decent reports.
When we have a file with code thats isn't required and without specs the coverage report is 100% because the framework doesn't reach this code.
To avoid this issue I have created a custom grunt task that will create a spec file without tests but requiring all js files from the project, excluding specs. This will make all files to be instrumented and coverage to be correctly generated.
grunt.registerTask('gen-instrumentation-file', function() { if(fs.existsSync(instrumentationFilePath)){ // remove file if exists fs.unlinkSync(instrumentationFilePath); } // use {'flags': 'a'} to append and {'flags': 'w'} to erase and write a new file var file = fs.createWriteStream(instrumentationFilePath, {'flags': 'a'}); grunt.log.writeln('generating instrumentation file: %s', instrumentationFilePath); var srcPath = './'; grunt.log.writeln("Source Path to walk: %s", srcPath); var specMatcher = grunt.config.data.jasmine_node.options.specNameMatcher; var filecheck = function(path){ var isModule = path.indexOf('node_modules') === 0; var isCoverage = path.indexOf('coverage') === 0; var isGruntfile = path === 'Gruntfile.js'; var isSpec = path.indexOf(specMatcher) !== -1; if(isModule || isCoverage || isGruntfile || isSpec) { return; } grunt.log.writeln("require file ./%s", path); file.write('require("./' + path + '");\n'); }; fsTools.walkSync(srcPath, '.js$', function(path,stats,callback){ filecheck(path); }); grunt.log.ok('generated %s', instrumentationFilePath); });
NOTE: This custom task depends on fs-tools npm package.
Now this task can be added into test pipeline, before running your tests or coverage to force instrumentation of all code.