我希望能够使用html-cov
记者,即使测试通过,所有覆盖值都是0.
我正在使用Grunt,伊斯坦布尔创建测试代码和mocha进行测试,这是我的grunt文件
module.exports = (grunt) ->
release = (type = 'develop') ->
switch type
when 'master'
grunt.task.run('release')
else
grunt.config.set('release',
options:
tag : false
pushTags: false
npm : false
)
grunt.task.run('release')
return
grunt.initConfig
pkg: grunt.file.readJSON 'package.json'
covervars:
base : 'tests/lib-cov'
build : '<%=covervars.base %>/build'
reports: '<%=covervars.base %>/reports'
watch:
coffee:
files: ['./src/**/*.coffee', './tests/**/*.spec.coffee']
tasks: ['build']
coffee:
compile:
options:
bare: true
expand : true
cwd : 'src'
src : '**/*.coffee'
dest : 'lib'
ext : '.js'
clean:
coverage: [
'<%=covervars.base %>/*'
]
instrument:
files : './lib/**/*.js'
options:
basePath: '<%=covervars.build %>/'
reloadTasks:
rootPath: '<%=covervars.build %>/lib/'
storeCoverage:
options:
dir: '<%=covervars.reports %>/'
makeReport:
src : '<%=covervars.reports %>/**/*.json'
options:
type : 'lcov',
dir : '<%=covervars.reports %>',
print: 'detail'
cafemocha:
test:
src : './tests/server/**/*.spec.coffee'
options:
require : ['./tests/common.coffee','./lib/']
ignoreLeaks: false
checkLeaks : true
colors : true
ui : 'bdd',
reporter : 'dot'
coverage:
src : './tests/server/**/*.spec.coffee'
options:
require : ['./tests/common.coffee','./<%=covervars.build %>/lib/']
globals : ['__coverage__']
ignoreLeaks: false
colors : true
ui : 'bdd',
reporter : 'spec'
coverage :
output: 'tests/lib-cov/coverage.html'
karma:
options :
configFile: 'tests/client/karma.conf.js'
unit :
browsers : ['Firefox']
singleRun: true
continuous:
browsers : ['Firefox', 'Chrome']
singleRun: false
grunt.loadNpmTasks 'grunt-release'
grunt.loadNpmTasks 'grunt-karma'
grunt.loadNpmTasks 'grunt-contrib-watch'
grunt.loadNpmTasks 'grunt-contrib-coffee'
grunt.loadNpmTasks 'grunt-cafe-mocha'
grunt.loadNpmTasks 'grunt-contrib-clean'
grunt.loadNpmTasks 'grunt-istanbul'
grunt.registerTask 'test', ['cafemocha:test']
grunt.registerTask 'cover', [
'coffee', 'clean',
'instrument',
#'reloadTasks',
'cafemocha:coverage',
#'storeCoverage', 'makeReport'
]
grunt.registerTask 'test:continuous', ['karma:continuous']
grunt.registerTask 'build', ['coffee', 'test']
grunt.registerTask 'releaseit', release
grunt.registerTask 'default', ['watch']
检测代码如下所示:http://pastebin.com/3wAt5VZT
istanbul lcov.info文件的一部分(正确生成)
TN:
SF:./lib/index.js
FNF:0
FNH:0
DA:1,1
DA:2,1
DA:10,1
DA:12,1
DA:14,1
DA:16,1
DA:18,1
DA:20,1
DA:22,1
DA:24,1
LF:10
LH:10
BRF:0
BRH:0
end_of_record
mocha似乎没有获得测试覆盖率,但如果我启用storeCoverage
和makeReport
,则instabul会向我显示正确的文件数和每个文件中使用的代码百分比。 mocha只能使用jscoverage的覆盖范围吗?如果没有,我是否缺少任何步骤?
答案 0 :(得分:4)
我创建了自己的grunt插件,正是这样做的。它执行mocha并通过stdin将其传递给istanbul。 https://github.com/pocesar/grunt-mocha-istanbul,可以通过npm npm install grunt-mocha-istanbul
它有一个名为coverage
的gruntjs事件,使您能够从已检测的代码中使用生成的lcov.info
答案 1 :(得分:1)
我使用BlanketJS而不是伊斯坦布尔来使用它。 Blanket不是创建源文件的单独的检测副本,而是实时评估覆盖范围。
为了让Blanket与Mocha和Grunt合作,我最终编写了自己的Grunt插件。该插件支持“强制执行”最低阈值,否则Grunt任务失败。
我写了一篇博客文章,其中包含所有详细信息:http://www.geekdave.com/2013/08/02/automated-code-coverage-enforcement-for-mocha-using-grunt-and-blanket/