How do I setup code coverage on my Express based API?

折月煮酒 提交于 2020-07-18 03:44:41

问题


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:

https://www.dropbox.com/s/qcgmout6hx91xgs/Screenshot%202014-04-19%2020.42.44.png

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:

enter image description here

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


回答1:


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)
    



回答2:


Based on my experience with istanbul, I think there might be a logic error in terms of what route is actually being used. Please set that test to use it.only and then look at the istanbul coverage. (There's no need to use the istanbul middleware, since your developers have access to the html output being written to the local filesystem.)

Please show what route is actually being covered, because istanbul is very clever in how it follows if statements.

If you have found a bug in istanbul, I encourage you to post a bug there.



来源:https://stackoverflow.com/questions/23177751/how-do-i-setup-code-coverage-on-my-express-based-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!