Question

I want to get the coverage of a functional test on an express endpoint. I have made this simple setup.

nodetest/index.js

module.exports = process.env.APP_COV
  ? require('./lib-cov')
  : require('./lib');

nodetest/lib/index.js

var express = require('express');
var app = express();
app.get('/hello.txt', function(req, res){
  res.send('Hello World');
});
var server = app.listen(3000, function() {
    console.log('Listening on port %d', server.address().port);
});

nodetest/test/simple.js

var request = require('supertest');

describe("Node Test Service",function(){
    it('Should return 200 OK trying to login', function (done) {
        request("http://localhost:3000")
            .get('/hello.txt')
            .expect(200)
            .end(function (err, res) {
            if(err)
                throw err;
            done();
            });
    });
});

nodetest/Makefile

all: test test-cov
test:
    @./node_modules/.bin/mocha -R xunit > reporter.xml

test-cov: lib-cov
    @APP_COV=1  ./node_modules/.bin/mocha -R html-cov > coverage.html

lib-cov:
    @./node-jscoverage/jscoverage lib lib-cov

node-jscoverage:
    @git clone git://github.com/visionmedia/node-jscoverage.git
    @cd node-jscoverage/ && ./configure && make


.PHONY: test

I am running the server with the env variable on and then fire the tests and it yields an empty coverage.html. Is it possible to get the coverage i am asking for?

Was it helpful?

Solution

I found out that in order to work, i had to start the express server right from the tests. I also needed to add a line on top of my tests

var lib = process.env['COV']?'../lib-cov' : '../lib';
//run express
var app = require(lib)

so that when you run the coverage test, you include the instrumented files.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top