Istanbul js Incorrect code coverage report because of required file

943 views Asked by At

Istanbul generates an artificially low coverage report because of required files that are tested in earlier code.

Example:

test.js

require('./test/aTests.js');
require('./test/bTests.js');
require('./test/cTests.js');

aTests.js or bTests.js

var a = require('../a.js);
// Test resulting in very high coverage of a.js

cTests.js

var c = require('../c.js);
// Tests covering every line of c.js

c.js

var a = require('./a.js');
var b = require('./b.js');

mobule.exports = class c {
    constructor() {
         // c module depends on a and b
         this.a = new a();
         this.b = new b();
    }
}

What I'm seeing is that I have nearly 100% code coverage on the rest of my codebase but when I add c.js to test.js the number rockets downwards. According to my lcov every line in the file is tested (its quite short) but because it depends on the majority of the codebase those required files report as untested the second time they are require()d.

Here are some screenshots.

With every file in the codebase. Testing with all files shows 66% of lines

Without the final file "c.js" Testing with all but the primary file shows 100% of lines

From looking at the number of lines tested in both cases it is clear that this is in error. I can also confirm this by stripping a and b from c as dependencies and checking coverage at 100%.

Has anyone found a solution to this? How can I accurately generate coverage for a module that depends on other modules in the library?

0

There are 0 answers