8

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

John Jerrby
  • 1,683
  • 7
  • 31
  • 68
  • are you willing to use `mocha` instead of `jasmine`? – Kevin Jul 14 '16 at 20:30
  • Remember that "code coverage" is just a metric that compares number of lines. https://www.thoughtworks.com/insights/blog/are-test-coverage-metrics-overrated It's easily gamed. – DigitalDesignDj Jul 14 '16 at 20:39
  • also remember that "testing" is just code that runs other code. It's easily gamed. – Kevin Jul 14 '16 at 20:41

1 Answers1

6

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']);
}
Kevin
  • 24,871
  • 19
  • 102
  • 158
  • HI Kevin thanks,I dont care to switch this is fine as soon as it work :) ,where should I put the path for the utils& controller folder? how I run the report – John Jerrby Jul 14 '16 at 20:40
  • Um, like I said, there are a lot of code coverage plugins out there that are super annoying to try to configure, either because they're under-documented or because the github page doesn't provide a working example. I was just letting you know that I happen to know of a decent coverage plugin that is really easy to get working. It's next to impossible to say why your example doesn't work, unless (someone) already has a working example for that plugin. – Kevin Jul 14 '16 at 20:44
  • and I've never used jasmine before, but in a quick read it appears they are extremely similar. So it might be to your benefit to just switch and fix any minor differences in your tests. – Kevin Jul 14 '16 at 20:46
  • Hi Kevin I want to use the plugin you mentions but where I put the reference to the tests folder like utils and controllers ? – John Jerrby Jul 14 '16 at 20:49
  • and since I newbe to grunt can you please provide the all code which should I use. – John Jerrby Jul 14 '16 at 20:53
  • Also I haven't used this particular option before, but the `includes` option looks like the one (or `excludes`, if that happens to be easier), to only include specific folders in the coverage report: https://github.com/pocesar/grunt-mocha-istanbul#optionsincludes – Kevin Jul 14 '16 at 21:20
  • HI Kevin 1+ ,its seems that something has start to work but I got error jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000; ^ ReferenceError: jasmine is not defined , how can I change my code and remove the jasmine(inside my grunt file- since my test runs mocha) – John Jerrby Jul 15 '16 at 10:36
  • Inside two test file (spec) I use jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000; and jasmine is not defined ?im using in the gruntfile https://github.com/mhevery/jasmine-node which I guess should be changed to mocha? do you know how? I update my question with the exact entry which I use – John Jerrby Jul 15 '16 at 10:42
  • inside your `describe` function you can do `this.timeout(20000)` which should achieve the same thing (for a specific test file). The default is 2000ms if you don't specify anything. Or you can pass a parameter to mocha (via mocha_istanbul) to set the default timeout across all tests. – Kevin Jul 15 '16 at 14:10
  • I updated. You just set `timeout:20000` in the options block in the `Gruntfile` – Kevin Jul 15 '16 at 20:38