Jest: Cannot read property 'coverageData' of null when trying to generate code coverage

Created on 11 Feb 2019  路  8Comments  路  Source: facebook/jest

馃悰 Bug Report

See reproduction repo for better understanding

When trying to generate a coverage with jest, the tests run as they should, but no coverage data is collected and there's a 'Cannot read property 'coverageData' of null' error in the console. This is likely because jest right now can't work with the unique folder structure that we have on our project (see the reproduction repo).

The folder where the package.json and node_modules are right next to the folder where all the source files are.
When the file to test and the test itself is in the same folder as the node_modules the coverage data is collected perfectly, however when it is in the folder next to the folder with the node_modules, it display the error mentioned in the first paragraph.

Feel free to close this if it's not a bug with jest, but at this point I cannot think of anything else that could be wrong here (other than our legacy folder structure).

To Reproduce

Steps to reproduce the behavior:

  • checkout this repo

  • run the commands in the project's README

  • see the console for the error message

Expected behavior

Expected the coverage data to be collected and put out to the console.

Link to repl or repo (highly encouraged)

reproduction repo link

Run npx envinfo --preset jest

Paste the results here:

System:
    OS: macOS 10.14.1
    CPU: (4) x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
  Binaries:
    Node: 10.15.1 - ~/.nvm/versions/node/v10.15.1/bin/node
    Yarn: 1.12.3 - /usr/local/bin/yarn
    npm: 6.7.0 - ~/.nvm/versions/node/v10.15.1/bin/npm
  npmPackages:
    jest: ^24.1.0 => 24.1.0 
Bug

Most helpful comment

@wfortin As a short term and quick solution, I rolled back to [email protected] and I am able to see coverage on all files and run tests as per normal. Once there is a fix, I will update Jest to v24. Note that if you do rollback, make sure to use setupTestFrameworkScriptFile (deprecated in v24) instead of setupFilesAfterEnv for your jest setup file location. See https://github.com/facebook/jest/issues/7758#issuecomment-459052457

All 8 comments

Perhaps related to #7758, except here it can't be because of a jest/babel-jest version mismatch

Yes I have looked at that issue, but as far as I can tell here the error is caused by something else, but sadly I can't really figure out what.

Thanks for the reproduction!

This is due to us ignoring files for coverage outside of rootDir: https://github.com/facebook/jest/blob/780c838e72eda7798d512adef12683d2d67de36b/packages/babel-jest/src/index.js#L112-L113 and https://github.com/facebook/jest/blob/780c838e72eda7798d512adef12683d2d67de36b/packages/jest-runtime/src/ScriptTransformer.js#L188-L189

When we call readInitialCoverage:
https://github.com/facebook/jest/blob/780c838e72eda7798d512adef12683d2d67de36b/packages/jest-cli/src/generateEmptyCoverage.js#L41
it can return null: https://github.com/istanbuljs/istanbuljs/blob/f1fb6886cb35093c1a2647c2d602a3dc1fb2fef5/packages/istanbul-lib-instrument/src/read-coverage.js#L48-L50


We should make sure to handle extracted being null as that's valid. However, I'd say that's just a symptom of the real issue, which is that the source code is not instrumented.

Manually setting cwd to path.resolve(config.rootDir, '..') (which is the actual root of your project) fixes it:

image

Thanks for the quick answer!! I wasted so many hours on this. I have a question though, could you show me how/where can i set the cwd to the right path? I can't figure it out myself.

Should I close this issue or do you plan on addressing it later?

Thanks again for the quick help.

Looks we are also facing this in a project that's structured as a monorepo. I tried hoisting Jest鈥檚 rootDir from the package dir to the repo dir, played around with testMatch paths etc., but could not come up a working solution. Either I see the exception reported in this issue, or the test coverage contains no files, or duplicate mocks are reported.

We'll have to stick with Jest 23 for now and keep using console.log('\n\n\n\n\n\n\n something \n\n\n\n\n\n\n') instead of just console.log('something') 馃槄

Is there any workaround while we wait for an official fix for this?

@wfortin As a short term and quick solution, I rolled back to [email protected] and I am able to see coverage on all files and run tests as per normal. Once there is a fix, I will update Jest to v24. Note that if you do rollback, make sure to use setupTestFrameworkScriptFile (deprecated in v24) instead of setupFilesAfterEnv for your jest setup file location. See https://github.com/facebook/jest/issues/7758#issuecomment-459052457

Duplicate of #7758

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Antho2407 picture Antho2407  路  3Comments

stephenlautier picture stephenlautier  路  3Comments

hramos picture hramos  路  3Comments

Secretmapper picture Secretmapper  路  3Comments

gustavjf picture gustavjf  路  3Comments