微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – Grunt代码覆盖率不起作用

我有以下grunt文件运行摩卡测试OK(我得到运行grunt.js后的测试结果)现在我想添加一个代码,我使用 https://github.com/taichi/grunt-istanbul模块.但是当我运行grunt.js什么也没有发生任何想法?

我想要的仅仅是在摩卡车测试运行之后,它会运行代码覆盖率与一些报告?任何新的代码覆盖将是巨大的

这是我的项目结构

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

这是我尝试过的咕噜声中的代码

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']);

};

如果有摩卡代码覆盖可以帮助它也会很好,我希望在我运行测试后,我可以看到所有代码覆盖的报告.

我想要覆盖文件夹utils和控制器(所有的文件),我应该如何配置?

UPDATE

这是我用于茉莉的事情,我想我应该改为摩卡

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

我该怎么改呢?
我的测试语法是相似的(茉莉/摩卡),我想要的只是运行我的测试和运行代码覆盖

解决方法

我会给你一个间接的答案.我已经获得了代码覆盖率,但是使用了不同的插件(和mocha).我不知道你是否愿意为摩卡车交换茉莉花,但是我会说,我在遇到这种情况之前,会勉强使用各种代码覆盖插件.我想你会同意配置是简洁明了的.

您想要的插件grunt-mocha-istbanbul,这里是您的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']);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐