AngularJS code converage

karma-coverage

 The easiest way is to keep karma-coverage as a devDependency in your package.json. More details refer to https://github.com/karma-runner/karma-coverage

{
  "devDependencies": {
    "karma": "~0.10",
    "karma-coverage": "~0.1"
  }
}

example karma_code_coverage.conf.js

module.exports = function(config){
  config.set({

      basePath : '../../',

      autoWatch : true,

      frameworks: ['jasmine'],

      browsers : ['Firefox'],

      plugins : [
            //'karma-chrome-launcher',
                'karma-firefox-launcher',
                'karma-jasmine',
                'karma-coverage'
            ],
            
      // Continuous Integration mode
      // if true, Karma captures browsers, runs the tests and exits
      singleRun : true,
    
      // test results reporter to use
      // possible values: 'dots', 'progress'
      // available reporters: https://npmjs.org/browse/keyword/karma-reporter
      reporters : ['progress', 'coverage'],
      coverageReporter:{
          type:'html',
          dir: 'test_out/angular/coverage/',
          subdir:'report'
      },
      // preprocess matching files before serving them to the browser
      // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
      preprocessors: {
          "src/js/angular/**/*.js":"coverage"
      }
  });
};

Gruntfile.js sample

module.exports = function (grunt) {

    grunt.loadNpmTasks('grunt-karma');
    grunt.loadNpmTasks('grunt-contrib-jshint');
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-uglify');

    grunt.initConfig({
        'pkg': grunt.file.readJSON('package.json'),

        'meta': {
            'jsFilesForTesting': [
                'src/bower_components/angular/angular.js',
                'src/bower_components/angular-route/angular-route.js',
                'src/bower_components/angular-resource/angular-resource.js',
                'src/bower_components/angular-animate/angular-animate.js',
                'src/bower_components/angular-sanitize/angular-sanitize.js',
                'src/bower_components/angular-mocks/angular-mocks.js',
                'src/bower_components/restangular/dist/restangular.js',
                'src/bower_components/underscore/underscore.js',
                'src/bower_components/jquery/jquery.js',
                'src/bower_components/bootstrap/dist/js/bootstrap.js',
                'src/js/usher/ajax.js',
                'src/js/lib/jquery.sparkline.js',
                'src/js/lib/ui-bootstrap-tpls-0.12.0.min.js',
                'src/js/ui-2/common/customized-modal.js',
                'test/angular/unit/**/*.js'
            ]
        },

        'karma': {
            'development': {
                'configFile': 'test/angular/karma_grunt.conf.js',
                'options': {
                    'files': [
                        '<%= meta.jsFilesForTesting %>',
                        'src/js/angular/**/*.js'
                    ]
                }
            },
            
            'coverage':{
                'configFile' : 'test/angular/karma_code_coverage.conf.js',  
                'options':{
                    'files':[
                         '<%= meta.jsFilesForTesting %>',
                         'src/js/angular/**/*.js'
                    ]
                }
            },

            'dist': {
                'options': {
                    'configFile': 'test/angular/karma_grunt_dist.conf.js',
                    'files': [
                        '<%= meta.jsFilesForTesting %>',
                        'dist/<%= pkg.name %>-<%= pkg.version %>.js'
                    ]
                }
            },

            'minified': {
                'options': {
                    'configFile': 'test/angular/karma_grunt_minified.conf.js',
                    'files': [
                        '<%= meta.jsFilesForTesting %>',
                        'dist/<%= pkg.name %>-<%= pkg.version %>.min.js'
                    ]
                }
            }
        },

        'jshint': {
            'options': {
                'reporter': require('jshint-jenkins-violations-reporter'),
                'reporterOutput': 'test_out/angular/jshint.xml',
                'force': true,
                'jshintrc': true
            },
            'beforeconcat': ['src/js/angular/**/*.js']
        },

        'concat': {
            'dist': {
                'src': ['src/js/angular/**/*.js'],
                'dest': 'dist/<%= pkg.name %>-<%= pkg.version %>.js'
            }
        },

        'uglify': {
            'options': {
                'mangle': false
            },
            'dist': {
                'files': {
                    'dist/<%= pkg.name %>-<%= pkg.version %>.min.js': ['dist/<%= pkg.name %>-<%= pkg.version %>.js']
                }
            }
        }
    });

    grunt.registerTask('test', ['karma:development','karma:coverage']);
    grunt.registerTask('coverage',['karma:coverage']);
    grunt.registerTask('build',
        [
            'karma:development',
            'karma:coverage',
            'jshint',
        ]);
};

  

posted on 2015-01-15 13:21  jmbkeyes  阅读(334)  评论(0编辑  收藏  举报

导航