如何在基于Express的API上设置代码覆盖率? [英] How do I setup code coverage on my Express based API?

查看:88
本文介绍了如何在基于Express的API上设置代码覆盖率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到这个问题已有一段时间了,我无法使现有的解决方案对我有用。



我有一个用Express编写的Node.js API。 js。我一直在使用Mocha,Chai和Supertest为API编写测试。这些测试主要是集成测试。



一个测试可能看起来像:

  it(在没有正确信息的情况下应该无法注册新用户,函数(完成){
api.post('/ user')
.send({})
.expect(400)
.expect('Content-Type',/ json /)
.end(function(err,res){
should.exist(res.body);
应该存在(res.body.error);
应该不存在(err);
res.body.error.should.contain('用户名');
res.body.error.should.contain('password');
done();
});
});

实际测试效果很好,但是现在我需要能够查看这些测试的代码覆盖率。我必须知道我没有充分测试。我尝试使用Mocha的测试范围:

  mocha -R html-cov --coverage> coverage.html 

伊斯坦堡:

 伊斯坦布尔封面_mocha--R spec-超时5000 

两者都遇到相同的问题:





您看,这是一个示例路由(用户注册)。我的测试肯定涵盖了它,但是由于它们没有直接调用此方法,因此覆盖工具假定它从未被调用。这就是问题所在-代码覆盖率工具无法捕获最终执行的代码。



我尝试了另一种解决方案-

http:/// /boycook.wordpress.com/2013/03/29/automated-javascript-testing-with-mocha-and-js-coverage-for-nodejs/

使用Mocha进行代码覆盖

解决方案

我也有同样的情况,我发现它必须按照我使用的方式进行操作超级测试




  • 在我直接针对正在运行的服务器测试我的应用之前,像这样。

      var request = require('supertest')
    var api = request('http://127.0.0.1 :3000')


  • 我通过需要我的快递应用来修复它>代替:

      var request = require('supertest')
    var app = require('../。 ./../')
    var api = request(app)



I've been at this problem for a while and I cannot make the existing solutions work for me.

I have a Node.js API written in Express.js. I have been writing tests for the API using Mocha, Chai, and Supertest. These test are mostly integration tests.

One test may look like:

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();
  });
});

The actual tests work great, but now I need to be able to view the code coverage of these tests. I have to know what I am not adequately testing. I have tried using Mocha's test coverage:

mocha -R html-cov --coverage > coverage.html

and Istanbul's:

istanbul cover _mocha -- -R spec --timeout 5000

Both of which suffer from the same issue:

You see, this is an example route (user registration). My tests definitely cover it, but since they do not call this method directly, the coverage tools assume it is never called. This is the problem - the code coverage tools aren't capturing the code that is eventually executed.

I tried another solution - the Istanbul Middleware, which actually seemed to capture the information better (although it was hacky). However the same route here looks like:

Which clearly is not desirable either. Surely other applications have run into this issue, how do they go about doing it?

Note: I've installed jscoverage as well to get all this to work.

Sources I've looked at:
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/
Code coverage with Mocha

解决方案

I just had this same exact situation and I found it has to do the way I was using supertest:

  • Before I was testing my app directly against my running server, like this.

    var request = require('supertest')
    var api = request('http://127.0.0.1:3000')
    

  • I fix it by requiring my express app instead:

    var request = require('supertest')
    var app = require('../../../')
    var api = request(app)
    

这篇关于如何在基于Express的API上设置代码覆盖率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆