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

2

15

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://static.mcmap.net/file/mcmap/ZG-AbGLDKwf1c1bQXGIvbFIvdC2jaRA/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

Hearten answered 20/4, 2014 at 2:21 Comment(5)
Why would you say the Istanbul Middleware output is "not desirable"? It shows that you're not testing the case where username && username.search(regexp) == -1, which is important information about your tests coverage!Fernferna
While the middleware may be analyzing the files and usage properly, the output is in such a garbled mess no one else but me will be able to understand it. It needs to be easy to understand.Hearten
I just understood your last image, sorry for the time loss. It's nonsense to do all that mess to get some coverage information.Fernferna
@Wayfarer, did you ever find a solution for this? I'm also running into some similar issues.Futile
I have the exact same issue with my tests, all my routes' functions are being ignored. Were you able to fix it?Letourneau
L
15

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)
    
Letourneau answered 5/10, 2015 at 23:40 Comment(0)
U
1

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.

Unworthy answered 21/4, 2014 at 13:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.