Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest Coverage All Files #1211

Closed
calclavia opened this issue Jun 26, 2016 · 20 comments
Closed

Jest Coverage All Files #1211

calclavia opened this issue Jun 26, 2016 · 20 comments

Comments

@calclavia
Copy link

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.

@missyjcat
Copy link

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.

@aaronabramov
Copy link
Contributor

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

@missyjcat
Copy link

@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);

@cpojer
Copy link
Member

cpojer commented Aug 3, 2016

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

@missyjcat
Copy link

yay!

@cpojer
Copy link
Member

cpojer commented Aug 16, 2016

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 jest@14.2.2-alpha.22bd3c33 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?

@cpojer cpojer closed this as completed Aug 16, 2016
@jsynowiec
Copy link
Contributor

jsynowiec commented Sep 12, 2016

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:

src
├── helpers
│   ├── array-from-mask.ts
│   └── index.ts
├── logger.ts
├── reporter.ts
└── server.ts

and tests:

test
├── 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.

jest@15.1.1

@aaronabramov
Copy link
Contributor

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

@jsynowiec
Copy link
Contributor

@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 |                |                   

@aaronabramov
Copy link
Contributor

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

@cpojer
Copy link
Member

cpojer commented Sep 12, 2016

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.

@jsynowiec
Copy link
Contributor

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

@umgupta
Copy link

umgupta commented Sep 15, 2016

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)`

@aaronabramov
Copy link
Contributor

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

@umgupta
Copy link

umgupta commented Sep 16, 2016

Thanks, that worked :)

@kirillgroshkov
Copy link

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.

@wrgoto
Copy link

wrgoto commented Sep 21, 2018

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?

@SimenB
Copy link
Member

SimenB commented Sep 22, 2018

I think that should work due to #5601?

@MannyDiera
Copy link

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/**',
  ],



CodesAreHonest pushed a commit to CodesAreHonest/abx-backend-assignment that referenced this issue Oct 8, 2020
jest coverage report ignores untested files by default, which results in incorrect coverage summary when some files lack tests. see also: https://jestjs.io/docs/en/configuration#collectcoveragefrom-array jestjs/jest#1211
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants