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.
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?