Any easy way to add build version info to coverage test results

543 Views Asked by At

I am using karma-coverage to measure the unit test coverage in my project and everything works in that regard just fine. I use HTML reporter into default directory.

However, I would need to "stamp" the coverage report with the build version information that I do have available using grunt-git-describe, which is currently used in the AngularJS app footer that loads the resulting version.json file. I didn't find any direct way to use this version.json file in the html reports from karma-coverage. So if anybody has a good idea how to do it, I would appreciate a lot.

Thanks in advance!

1

There are 1 best solutions below

0
On

I did manage to implement this with a sort-of work around. I use text-replace module in grunt after running karma to do this. If some one has a better solution, please share as this is a bit of a hack, but it works ok. As karma-coverage's html reports in my environment goes to the projects' root /coverage/ folder, I made a recursive text replace into every .html file there looking for the default footer and adding my version info there...

First, installed grunt-text-replace

$ npm install grunt-text-replace --save-dev

Then I made a following replace function in gruntfile.js:

grunt.initConfig({
    replace: {
        coverage: {
            src: ['coverage/**/*.html'],
            overwrite: true,
            replacements: [
            {
                from: '<div class="meta">Generated by',
                to: function(){return grunt.config.get('task.replace.versionString');}
            }
            ]
        }
    },
// and your other stuff in initConfig()

And I added a new task into grunt for this:

grunt.registerTask('coverage', 'Adds version info to coverage results', function(){
    grunt.task.requires('version'); // The 'version' task creates a 'version.json' file
    var vers = grunt.file.readJSON('version.json');
    // Set the desired string to be used in text-replace -function
    grunt.config.set('task.replace.versionString', '<div class="meta">Version ' + vers.version + ', Tag "' + vers.revision[0] + '"<br>Generated by');
    grunt.task.run(['replace']);
});

A bit ugly but works like a charm ;-)