Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grunt code coverage doesn't work

I've the following grunt file which runs the mocha tests OK (I get results of the test after running grunt.js)Now I want to add a code and I use the https://github.com/taichi/grunt-istanbul module. but when I run the grunt.js nothing happen,any idea?

What I want is simply after that mocha test are running it will run the code coverage with some reports? any new code coverage will be great

This is my project structure

myApp
 -server.js
 -app.js
 -test
   -test1.spec
   -test2.spec
 -test-reports
 -grunt.js
 -utils
  -file1.js
  -file2.js
 -controller
  -file1.js
  -file2.js

This is the code inside the grunt for what I've tried

module.exports = function (grunt) {

    var path = require('path');

    process.env.RESOURCE_PATH_PREFIX = "../";
    var d = new Date();

    var datestring = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear() + " " +
        d.getHours() + ":" + d.getMinutes();

    var npmCommand = path.dirname(process.execPath).concat('/npm');
    var reportDir = "./test-reports/" + datestring;


    grunt.initConfig({
        jasmine_nodejs: {
            // task specific (default) options
            options: {
                specNameSuffix: ["-spec.js"], 
                helperNameSuffix: "helper.js",
                useHelpers: false,
                stopOnFailure: false,
                reporters: {
                    console: {
                        colors: true,            
                    },
                    junit: {
                        savePath: "./test-reports",
                        filePrefix: "testresult",
                    }
                }
            },
            test: {
                specs: [
                    "test/*",
                ]
            },
            makeReport: {
              src: './test-reports/coverage.json',//should I create this file?or its created automatically ?
               options: {
                 type: ['lcov', 'html'],
                   dir: reportDir,
                   print: 'detail'
        }
    },
    coverage: {
        APP_DIR_FOR_CODE_COVERAGE: "./utils/*.js",//HERE IS THE PATH OF THE UTILS Folder which all the js login which I want to test there
        clean: ['build'],
        instrument: {
            files: tasks,//WHAT IS TASKS????
            options: {
                lazy: true,
                basePath: 'build/instrument/'//I DONT KNOW WHAT IT IS???
            }
        },
        reloadTasks: {
            rootPath: 'build/instrument/tasks'//SHOULD I USE IT????
        },
        storeCoverage: {
            options: {
                dir: reportDir
            }
        }
    }
    });


    grunt.loadNpmTasks('grunt-jasmine-nodejs');
    grunt.loadNpmTasks('grunt-istanbul');
    grunt.registerTask('default', ['jasmine_nodejs']);
    grunt.registerTask('cover', ['instrument', 'test',
        'storeCoverage', 'makeReport']);

};

If there is mocha code coverage which can help it will be great either, I want that after I run the test I will able to see report with all the code coverage.

I want that the coverage will be done for folder utils and controller (all the files there) how should I config that?

UPDATE

This is what I use for jasmin and I think I should change to mocha

jasmine_nodejs: {
            // task specific (default) options
            options: {
                specNameSuffix: ["-spec.js"], // also accepts an array
                helperNameSuffix: "helper.js",
                useHelpers: false,
                stopOnFailure: false,
                reporters: {
                    console: {
                        colors: true,
                        cleanStack: 1,       // (0|false)|(1|true)|2|3
                        verbosity: 4,        // (0|false)|1|2|3|(4|true)
                        listStyle: "indent", // "flat"|"indent"
                        activity: false
                    },
                    junit: {
                        savePath: "./test-reports",
                        filePrefix: "testresult",
                        consolidate: true,
                        useDotNotation: true
                    }
                }
            },
            test: {
                // target specific options
                options: {
                    useHelpers: false
                },
                // spec files
                specs: [
                    "test/*",
                ]
            }
        },

How should I change it? The syntax of my test are similar(jasmine/mocha) and what I want is simply to run my test and after run the code coverage

like image 814
John Jerrby Avatar asked Jul 12 '16 20:07

John Jerrby


1 Answers

I'll give you an indirect answer. I've gotten code coverage to work before, but with a different plugin (and with mocha). I'm not sure if you're open to swapping out jasmine for mocha but I will say that I struggled with various code coverage plugins before coming across this one. I think you'll agree the configuration is both concise and obvious.

The plugin you want is grunt-mocha-istbanbul, and here is a sample configuration for your Gruntfile:

module.exports = function(grunt) {
  grunt.initConfig({
    clean: ['coverage'],
    mocha_istanbul: {
      coverage: {
        src: 'test',
        options: {
          timeout: 20000,
          'report-formats': 'html',
          print: 'summary',
          check: {
            lines: 90,
            statements: 90,
            functions: 100,
            branches: 80
          }
        }
      }
    }
  });
  grunt.loadNpmTasks('grunt-contrib-clean');
  grunt.loadNpmTasks('grunt-mocha-istanbul');
  grunt.registerTask('default', ['clean', 'mocha_istanbul']);
}
like image 109
Kevin Avatar answered Oct 20 '22 18:10

Kevin