我最近在Mocha的帮助下开始进行Node项目的单元测试.到目前为止事情进展顺利,我发现我的代码已经有了很大的改进,因为我正在考虑在我的测试中涵盖的所有角度.
现在,我想与我的团队其他成员分享我的经验,并让他们自己进行测试.我想分享的部分信息是我的代码实际涵盖了多少.
下面是我的应用程序结构示例,我将其分成不同的组件或模块.为了促进重用,我试图将所有依赖项保持在最低限度并与组件文件夹隔离.这包括保持测试隔离,而不是test/项目根目录中的默认文件夹.
| app/
| - component/
| -- index.js
| -- test/
| ---- index.js
Run Code Online (Sandbox Code Playgroud)
目前我package.json看起来像这样.我正在和伊斯坦布尔一起玩,但我与之无关.我也尝试过使用Blanket,取得了类似的成功.
{
"scripts": {
"test": "clear && mocha app/ app/**/test/*.js",
"test-cov": "clear && istanbul cover npm test"
}
Run Code Online (Sandbox Code Playgroud)
如果我test-cov按原样运行我的命令,我从伊斯坦布尔得到以下错误(这没有帮助):
No coverage information was collected, exit without writing coverage information
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:鉴于我目前的应用程序结构和环境,如何使用Istanbul(或其他工具)正确报告我的代码覆盖率?
如何使用Node,Mocha和我当前的应用程序结构报告我的代码覆盖率?
需要说明的是,Mocha正在当前状态下正确运行测试.获取代码覆盖率报告是我努力工作的原因.
我收到通知,其他问题可能已经回答了我的问题.它只建议安装伊斯坦布尔并运行cover我已经完成的命令.另一个建议建议运行测试命令_mocha,我从一些研究中做的是阻止伊斯坦布尔吞下摩卡的标志,在新版本的摩卡中不是必需的.
目前伊斯坦布尔只为我的测试中使用的文件生成了覆盖范围,这是可以的,但似乎无法实现覆盖的目的.
我没有Istanbul配置,并通过npm test以下脚本字符串调用它:
$ istanbul cover _mocha -- -R dot --check-leaks --recursive test/
有没有办法为我的所有源代码生成报道?
如何设置karma测试运行器以生成打字稿项目的代码覆盖率报告?
鉴于以下文件夹结构和karma.conf.js文件,我已经在使用karma来运行用TypeScript编写的测试.
我已经与周围的摆弄karma-coverage和remap-istanbul,但没有任何运气呢.如果可能的话,我想在没有任何额外的情况下这样做npm scripts.
.
??? karma.conf.js
??? package.json
??? src
? ??? add.ts
??? test
? ??? addSpec.ts
??? tsconfig.json
??? typings
? ??? globals
? ??? index.d.ts
??? typings.json
Run Code Online (Sandbox Code Playgroud)
karma.conf.js
var istanbul = require('browserify-istanbul');
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['mocha', 'chai', 'sinon', 'browserify'],
files: [
'test/**/*Spec.ts'
],
exclude: [
],
preprocessors: {
'test/**/*Spec.ts': ['browserify']
},
browserify: {
debug: true,
plugin: ['tsify'],
transform: [
istanbul({irgnore: ['**/node_modules/**']})
]
},
reporters: ['progress', …Run Code Online (Sandbox Code Playgroud) 我创建了一个应用程序,create-react-app并在Jest + Istanbul中编写了一些UI测试.
我想获得这些UI测试的代码覆盖率.我喜欢继续使用jest,因为我已经将它用于单元测试.
create-react-app如果可能的话,我不想弹出.但如果别无选择,我会对此持开放态度.
到目前为止我尝试过的:
在 package.json
"scripts": {
"uitest": "react-scripts test --env=jsdom --verbose --testMatch='**/*.ui-test.{js}'",
}
Run Code Online (Sandbox Code Playgroud)
如果我跑 npm run uitest -- --coverage
^我认为在上面的场景中它只捕获测试而不是实际的App.
我该如何解决?
其他尝试失败:
1)如何覆盖伊斯坦布尔的React jsx文件? - 不要像我使用的那样申请create-react-app
2)https://github.com/facebook/create-react-app/issues/3257 - 显然这个功能被提出但被拒绝了.
3)https://github.com/istanbuljs/puppeteer-to-istanbul/issues/18 - 有一个名为puppeteer-to-istanbul的库,但它不支持源映射.(参见问题链接)
4)我也查看了关于safaribooks的Node.js Web开发 - 第四版的书 - 我找到了一个有用的Puppeteer指南,但它似乎没有涵盖,代码覆盖率.
5)在safaribooks上预订动手持续集成和交付 - 有一个关于Puppeteer + Jest测试的部分,没有说明代码覆盖率.
6)我试过puppeteer-to-istanbul- >我们可以通过这种方式计算bundle的代码覆盖率,它不支持源映射.
7)尝试了Enselic的建议,但无法使其正常工作.push尝试推送时,它似乎在自定义预设中的方法崩溃babel-plugin-istanbul.
我正在尝试将现有的测试流程集成到现在包含React,但我正在努力解决代码覆盖问题.通过以下项目/教程,我已经能够使我的单元测试工作正常 - https://github.com/danvk/mocha-react - http://www.hammerlab.org/2015/02/14/testing -react-Web的应用程序与-摩卡/
我一直在使用伊斯坦布尔来覆盖我的节点代码,它运行得很好.但是,我无法覆盖我在测试中使用的jsx文件.
这是一个现有的伊斯坦布尔任务的例子,它也可以在vanilla js上运行(节点后端代码)
var mocha = require('gulp-mocha');
var istanbul = require('gulp-istanbul');
gulp.task('test-api', function (cb) {
gulp.src(['api/**/*.js'])
.pipe(istanbul()) // Covering files
.pipe(istanbul.hookRequire()) // Force `require` to return covered files
.on('finish', function () {
gulp.src(['test/api/*.js'])
.pipe(mocha())
.pipe(istanbul.writeReports()) // Creating the reports after tests runned
.on('end', cb);
Run Code Online (Sandbox Code Playgroud)
我的问题(我认为)是我无法让伊斯坦布尔识别jsx文件,或者他们没有与测试中运行的文件进行比较.我尝试使用gulp -react模块将jsx预编译为js,以便伊斯坦布尔可以使用它,但我不确定它是否正常工作.它没有以某种方式被覆盖,我不确定问题出在哪里.
var mocha = require('gulp-mocha');
var istanbul = require('gulp-istanbul');
var react = require('gulp-react');
gulp.task('test-site-example', function (cb) {
gulp.src(["site/jsx/*.jsx"]) //Nothing is being reported by Istanbul (not being picked up) …Run Code Online (Sandbox Code Playgroud) 我想让伊斯坦布尔工作.
我一直在运行伊斯坦布尔的时候收到这条消息:
没有收集覆盖信息,退出时没有编写覆盖范围信息
我已经尝试了我在网上找到的所有东西,你可以看到:
"scripts": {
"start": "node ./bin/start.js",
"test": "mocha test --no-timeouts",
"debug_mocha": "node-debug --no-timeouts _mocha",
"eslint": "eslint .",
"jshint": "jshint --exclude ./node_modules .",
"istanbul": "istanbul cover --include-all-sources --hook-run-in-context node_modules/.bin/_mocha -- -u exports -R spec test/**/*",
"istanbul2":"istanbul cover node_modules/.bin/_mocha -- -u exports -R spec test/**/*",
"istanbul1":"istanbul cover node_modules/.bin/_mocha -- test/**/*",
"istanbul0":"istanbul cover _mocha test/**/*.js",
"istanbul3":"istanbul cover _mocha -- -R spec --recursive test"
}
Run Code Online (Sandbox Code Playgroud)
我的.istanbul.yml文件位于项目的根部,看起来像伊斯坦布尔正在成功地获取它.
//.istanbul.yml
instrumentation:
compact: false
save-baseline: true
reporting:
reports:
- lcov
- cobertura
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
我正在使用gulp-istanbul通过Gulp生成JavaScript单元测试覆盖率报告.有没有办法配置伊斯坦布尔生成我的gulp流中所有JS文件的完整覆盖率报告,而不仅仅是测试用例触及的文件.
我正在开发一个包含大量JS的项目,但没有单元测试,我们正在努力提高测试覆盖率.我希望有一个覆盖率报告,从大多数文件的0%覆盖率开始,但随着时间的推移,覆盖率将会提高.
gulp.task( 'test', function () {
gulp.src( [ my source glob ] )
.pipe( istanbul() )
.on( 'end', function () {
gulp.src( [ my test spec glob ] )
.pipe( mocha( {
reporter: 'spec'
} ) )
.pipe( istanbul.writeReports(
[ output location ]
) );
} );
} );
Run Code Online (Sandbox Code Playgroud) 我已经解决了这个问题一段时间了,我不能让现有的解决方案适合我.
我有一个用Express.js编写的Node.js API.我一直在使用Mocha,Chai和Supertest为API编写测试.这些测试主要是集成测试.
一个测试可能看起来像:
it('should fail to register a new user without the proper information', function(done) {
api.post('/user')
.send({})
.expect(400)
.expect('Content-Type', /json/)
.end(function(err, res) {
should.exist(res.body);
should.exist(res.body.error);
should.not.exist(err);
res.body.error.should.contain('Username');
res.body.error.should.contain('password');
done();
});
});
Run Code Online (Sandbox Code Playgroud)
实际测试工作得很好,但现在我需要能够查看这些测试的代码覆盖率.我必须知道我没有充分测试.我尝试过使用Mocha的测试覆盖率:
mocha -R html-cov --coverage > coverage.html
Run Code Online (Sandbox Code Playgroud)
和伊斯坦布尔的:
istanbul cover _mocha -- -R spec --timeout 5000
Run Code Online (Sandbox Code Playgroud)
两者都遭遇同样的问题:

你看,这是一个示例路由(用户注册).我的测试肯定会覆盖它,但由于它们不直接调用这种方法,因此覆盖工具假设它从未被调用过.这就是问题 - 代码覆盖工具没有捕获最终执行的代码.
我尝试了另一种解决方案 - 伊斯坦布尔中间件,它实际上似乎更好地捕获了信息(尽管它很笨拙).然而,这里的相同路线如下:

这显然也是不可取的.当然其他应用程序遇到了这个问题,他们如何去做呢?
注意:我已经安装了jscoverage以使所有这些工作.
我看过的来源:
https://brianstoner.com/blog/testing-in-nodejs-with-mocha/
http://boycook.wordpress.com/2013/03/29/automated-javascript-testing-with -mocha-and-js-coverage-for-nodejs/
Mocha的代码覆盖率
在ES5中,我们可以这样写:
function(a){
/* istanbul ignore next */
a = a || 123;
}
Run Code Online (Sandbox Code Playgroud)
怎么忽略在ES6?
function(a = 123 ){
}
Run Code Online (Sandbox Code Playgroud)
我试过这个:
function(/* istanbul ignore next */a = 123 ){
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
istanbul ×10
mocha.js ×6
node.js ×4
javascript ×3
gulp ×2
reactjs ×2
unit-testing ×2
babeljs ×1
browserify ×1
chai ×1
ecmascript-6 ×1
express ×1
jestjs ×1
karma-runner ×1
puppeteer ×1
sinon ×1
testing ×1
typescript ×1