When using collectCoverage, I only get coverage for files that are imported and tested (and not mocked). And it works fine for those files, but if I then add a new file to my project, and do not write any tests for it, it does not show up as not covered. The coverage results make it look like you have a lot higher coverage than you really do.
Is there any way to force it to include coverage for all js files within my source? So that if someone adds a new file, the coverage results will include that file as uncovered in the results?
I never found a way, ended up writing a script that runs through and does a require of every file in our app directory.
Im having the same problem, and I dont think that collectCoverageOnlyFrom is a good solution :)
+1
I'm about to do @browniefed 's plan but it's a bit of a sigh that it's necessary
I'd be open to accepting a pull request if someone finds a sane way to do this. I expect it to be pretty slow though if you run this as a separate step. I guess what you are looking for here is do what @browniefed suggested - write a test that requires an entire app. Since you need basic instrumentation I don't think there will be any good generic way to do this other than writing a test itself.
Feel free to reopen if you want to submit a pull request but I'm probably not going to have time to build this anytime soon myself.
+1
I feel like I heard recently that this was actually implemented somehow, is that the case? Or is there any existing module that makes this easier than just doing a glob + require myself?
Alrighty, for posterity, here's what I have. I hope it's helpful:
/**
* Without this file, files that have no tests
* aren't included in test coverage metrics.
* This file remedies that and ensures that untested
* files are counted against us in the coverage metrics.
* This helps keep us get a true picture of the overall
* code coverage of the project.
*/
import { resolve } from 'path'
import glob from 'glob'
test('dynamically require project files', () => {
const globToCover = resolve(__dirname, './**/!(*.test).js')
const filesToCover = glob.sync(globToCover)
// if we don't have any filesToCover, then something's probably wrong...
expect(filesToCover.length > 0, `there should be at least a handful of files in our codebase`)
// if we have too many files to cover then something's probably wrong too...
expect(filesToCover.length < 25, `there shouldn't be this many files in our codebase`)
// dynamically require files
filesToCover.forEach((file) => {
try {
require(file)
} catch (error) {
// ignore the error.
// our purposes of getting the file
// insturmented for coverage have been accomplished
// by simply requiring the file.
}
})
})
collectCoverageFrom can be used for this, it takes globs to match files and then runs coverage on all files in the project that match.
Wonderful! That totally works. So much better. Thanks!
Most helpful comment
collectCoverageFromcan be used for this, it takes globs to match files and then runs coverage on all files in the project that match.