Jest: Jest Coverage All Files

Created on 26 Jun 2016  ยท  19Comments  ยท  Source: facebook/jest

When Jest performs code coverage, it seems to only cover the files that were tested instead of all files in my source directory. This gives a skewed percentage of files tested.

For example, if I have 10 source files and I only test one of them, the coverage report will not include all the files. It would be nice if there's a way to specify which files should be covered. collectCoverageOnlyFrom does not seem to serve this purpose.

Most helpful comment

hey @umgupta
collectCoverageFrom accepts a list of globs, and not regular expersions
so in your config instead of

"collectCoverageFrom" : ["app\/.*\\.js"],

you should have

"collectCoverageFrom" : ["app/**/*.js"],

you can find globs documentation here https://github.com/sindresorhus/multimatch

All 19 comments

I'm also looking for this functionality. Our code coverage reports would be much more useful if they reported on all the files we're targeting for coverage.

we're working on it right now, so probably in a few weeks that should be implemented

@calclavia my hacky way around this was to generate a jest test before running code coverage that requires every file that I'm interested in instrumenting.

Not ideal but works for my CI for now.

var fs = require('fs');
var glob = require('glob');
var path = require('path');

var ROOT_PATH = path.resolve(__dirname, '..', 'app');

// NOTE: These are matched against absolute paths of files
var EXCLUDE_LIST = [
    '__mocks__', // Don't instrument testing files
    'jest.js', // Don't instrument testing files
];
var files = glob.sync(`${ROOT_PATH}/**/*.js`);

var out = 'jest.disableAutomock();\n';
for (var i = 0; i < files.length; i++) {
    var filePath = files[i];
    var modulePath = `./${path.relative(ROOT_PATH, filePath)}`.slice(0, -3);
    var regex = new RegExp(`(${EXCLUDE_LIST.join('|')})`);
    if (!regex.test(filePath)) {
        out += `
            try {
                require('${modulePath}');\n
            } catch(e) {
                console.log('"${modulePath},"');
                console.log(e);
            }
        `
    }
}

out += `

it('Requires all the files in order to instrument them for code coverage', () => {
    expect(1).toEqual(1);
});

`;

fs.writeFileSync(`${ROOT_PATH}/requireAll.jest.js`, out);

See #1354. This will be in the next major release of Jest. Woohooo!

yay!

This is now part of the latest Jest pre-release and will be in Jest 15. @dmitriiabramov rewrote Jest's code coverage support. Please try with jest@test or [email protected] to see if this issue still persists. If it does, we'll reopen this issue.

The new option is collectCoverageFrom and it takes glob patterns. @dmitriiabramov can you make sure this is part of the API documentation before 15 comes out?

The collectCoverageFrom seems to be still missing from the API documentation. Also I can't get this working.

For e.g. given the following source files:

โ”œโ”€โ”€ helpers
โ”‚ย ย  โ”œโ”€โ”€ array-from-mask.ts
โ”‚ย ย  โ””โ”€โ”€ index.ts
โ”œโ”€โ”€ logger.ts
โ”œโ”€โ”€ reporter.ts
โ””โ”€โ”€ server.ts

and tests:

โ”œโ”€โ”€ helpers
โ”‚ย ย  โ””โ”€โ”€ array-from-mask.spec.ts
โ”œโ”€โ”€ logger.spec.ts
โ””โ”€โ”€ reporter.spec.ts

If i set the collectCoverageFrom to "collectCoverageFrom": ["src/**/*.{ts,js}"], the server.ts file is still missing from the coverage report.

[email protected]

@jsynowiec do you have an example project that i can look at?
we have an integration test for .ts coverage that seems to be working https://github.com/facebook/jest/blob/master/integration_tests/typescript-coverage/package.json

and what does your full config look like?

@dmitriiabramov yes, you can check my jsynowiec/node-typescript-boilerplate. In my current project Jest configuration is the same.

You can clone that repository and add a new file (cp src/main.ts src/another.ts), then add "collectCoverageFrom": ["src/**/*.{ts,js}"], to jest config in package.json. After this run npm run t:c.

I'm getting following output

 PASS  test/main.spec.ts                                                                   
  main module                                                                              
    โœ“ should export greeter function (3ms)                                                 
    greeter function                                                                       
      โœ“ should greet a user (1ms)                                                          

----------|----------|----------|----------|----------|----------------|                   
File      |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |                   
----------|----------|----------|----------|----------|----------------|                   
All files |      100 |      100 |      100 |      100 |                |                   
 main.ts  |      100 |      100 |      100 |      100 |                |                   
----------|----------|----------|----------|----------|----------------|                   
Test Summary                                                                               
 โ€บ Ran all tests.                                                                          
 โ€บ 2 tests passed (2 total in 1 test suite, run time 2.854s)  

but I expected to also see

 another.ts  |          0 |          0 |          0 |          0 |                |                   

@jsynowiec i could reproduce the issue and was able to fix it by removing

    "testPathDirs": ["<rootDir>/test"],

from the config.

@cpojer if we have testPathDirs in package.json, hast-map doesn't seem to have any files outised of the test directory, is that intentional?

testPathDirs is unfortunately the worst name someone gave to this field ever. It simply means roots, ie. that Jest will only look into folders specified in testPathDirs. The default is rootDir.

@cpojer @dmitriiabramov thanks for clarifying this. API documentation is currently a bit misleading.

A list of paths to directories that Jest should use to search for tests in.

hi @jsynowiec Were you able to find a fix ? My jest configuration is -

"jest": {
    "moduleNameMapper": {
      "^.*[.](jpg|JPG|gif|GIF|png|PNG|svg|SVG|scss|SCSS|sass|SASS|less|LESS|css|CSS)$": "tests/empty_module"
     },
    "modulePaths" : ["./"],
    "bail": true,
    "coverageDirectory": "coverage",
    "collectCoverageFrom" : ["app\/.*\\.js"],
    "collectCoverage": "true",
    "coverageThreshold": {
      "global": {
        "branches": 50,
        "functions": 50,
        "lines": 50,
        "statements": 50
      }
    },
    "testRegex": "tests/.*(test)\\.js$"
  },

app has a folder main and lots of folder inside. And tests folder follow the same structure.
I have written test case for just one file and I get this report.

`PASS tests/main/components/PrimaryNav.test.js
โœ“ Renders PrimaryNav with user prop (9ms)

----------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch| % Funcs | % Lines |Uncovered Lines|
----------|----------|----------|----------|----------|----------------|
All files |Unknown |Unknown |Unknown |Unknown | |
----------|----------|----------|----------|----------|----------------|
Test Summary
โ€บ Ran all tests.
โ€บ 1 test passed (1 total in 1 test suite, run time 1.49s)`

hey @umgupta
collectCoverageFrom accepts a list of globs, and not regular expersions
so in your config instead of

"collectCoverageFrom" : ["app\/.*\\.js"],

you should have

"collectCoverageFrom" : ["app/**/*.js"],

you can find globs documentation here https://github.com/sindresorhus/multimatch

Thanks, that worked :)

oh, I'm so lucky to find that solution today! "collectCoverageFrom" : ["app/**/*.js"], worked for me! I thought it's a design limitation of jest and was very upset, but now I can proceed and setup KPIs for test coverage.

When I use the Press p to filter by a filename regex pattern. feature in watch mode, is it default behavior to run coverage for all untested files? When I'm filtering in watch mode, I only want to see the coverage for the one test file I'm running, not my whole codebase.

How do I change this?

I think that should work due to #5601?

In case anyone still having issues. I am working in Vue and was also getting empty code coverage reports.

I changed the pattern to find the files and it works now. Here is my setup.

```
collectCoverageFrom: [
'src//.{js,vue}',
'!
/node_modules/',
'!
/vendor/
*',
],

Was this page helpful?
0 / 5 - 0 ratings