标题说明了一切。尽管搜索了互联网,但我还没有找到解决此问题的单个示例。
这里有一些有惊无险
- https://github.com/amitayd/grunt-browserify-jasmine-node-example - grunt、browserify 和 jasmine
- https://github.com/gotwarlost/istanbul/issues/59#issuecomment-18799734 - browserify 和 Istanbul 尔
这是我正在进行的代码 https://github.com/wheresrhys/on-guard/tree/browserify (注意它是'browserify' 分支——Gruntfile.js 有点乱,但很快就会整理好)。我使用 console.log
进行的初步调查表明,bundle.src.js
以某种方式被加载到页面中,但是当测试运行(并通过!)时,bundle.src.js
没有运行,所以我觉得这可能是一个别名问题......虽然这个问题仅限于 phantomjs,因为当我在 chrome 中打开 specrunner 时,代码正在运行.
最佳答案
我正在使用 grunt-browserify
+ browserify-istanbul
+ grunt-contrib-jasmine
+ grunt-template-jasmine -istanbul
作为解决方案。在使用 browserify
构建源文件时,此解决方案还排除了第三方库。
先展示代码,稍后再解释,
grunt.initConfig({
browserify: {
// build specs using browserify
specs: {
src: ["spec/**/*Spec.js"],
dest: "spec/build/specs.js",
options: {
debug: true
}
},
// build source files using browserify and browserify-istanbul
dev: {
options: {
debug: true,
browserifyOptions: {
standalone: 'abc'
},
transform: [['browserify-istanbul', {
ignore: ['**/node_modules/**'], // ignore third party libs
defaultIgnore: true
}]]
},
src: ['abc.js'],
dest: 'dist/abc.js'
}
},
connect: {
server: {
options: {
port: 7000
}
}
},
// test using jasmine, generate coverage report using istanbul
jasmine: {
coverage: {
src: ['dist/abc.js'],
options: {
junit: {
path: 'bin/junit'
},
host: 'http://localhost:7000/',
specs: 'spec/build/specs.js',
keepRunner: true,
summary: true,
template: require('grunt-template-jasmine-istanbul'),
templateOptions: {
replace: false, // *** this option is very important
coverage: 'bin/coverage/coverage.json',
report: [
{
type: 'html',
options: {
dir: 'spec/coverage/html'
}
}]
}
}
}
}
grunt.registerTask('specs', ['browserify:specs', 'browserify:dev', 'connect', 'jasmine']);
生成 Istanbul 尔覆盖率报告的步骤可以归纳为三个:
- 仪器代码
- 运行测试
- 生成覆盖率报告
在我们的解决方案中,我们在步骤 1 中使用 browerify-istanbul
,grunt-contrib-jasmine
和 runt-template-jasmine-istanbul
在第 2 步和第 3 步中。
browserify-istanbul
将让您在 browserify 构建步骤中检测代码,这样我们就可以轻松忽略第三方库。但是 grunt-template-jasmine-istanbul
将再次检测代码。为避免这种情况,您可以在选项中将 replace
设置为 false
。
引用:
关于node.js - 为在 phantomjs 中的 browserify 包上运行(通过 grunt)的 Jasmine 测试生成 Istanbul 尔代码覆盖率报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20647019/