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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With