Grunt代码覆盖不起作用

Joh*_*rby 8 javascript mocha.js node.js gruntjs istanbul

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

我想要的只是在mocha测试运行之后它会运行代码覆盖率和一些报告吗?任何新的代码覆盖都会很棒

这是我的项目结构

myApp
 -server.js
 -app.js
 -test
   -test1.spec
   -test2.spec
 -test-reports
 -grunt.js
 -utils
  -file1.js
  -file2.js
 -controller
  -file1.js
  -file2.js
Run Code Online (Sandbox Code Playgroud)

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

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

};
Run Code Online (Sandbox Code Playgroud)

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

我希望覆盖率将完成文件夹utils和控制器(那里的所有文件)我该如何配置?

UPDATE

这是我用于jasmin的东西,我想我应该改为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/*",
                ]
            }
        },
Run Code Online (Sandbox Code Playgroud)

我该怎么改变它? 我的测试语法类似(jasmine/mocha),我想要的只是运行我的测试并在运行代码覆盖后

Kev*_*vin 5

我会给你一个间接的答案.我之前已经开始使用代码覆盖,但使用了不同的插件(以及mocha).我不确定你是否愿意更换jasmine,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']);
}
Run Code Online (Sandbox Code Playgroud)