Jest tries to report coverage on untested JSON files and throws error

Viewed 382

Link to demo on replit.com showing the error: https://replit.com/@chipit24/jest-playground.

When run, the test passes but I see the following unexpected error in the output:

Running coverage on untested files... Failed to collect coverage from foo.json
ERROR: foo.json: Missing semicolon

Here is my jest.config.json:

module.exports = {
  collectCoverage: true,
  collectCoverageFrom: ['<rootDir>/**'],
  coveragePathIgnorePatterns: ['package.json', 'package-lock.json', 'node_modules', 'coverage'],
  transform: {
    '.+\\.json': '<rootDir>/jsonTransform.js',
  },
};

The Jest docs for collectCoverageFrom mention (emphasis mine):

If a file matches the specified glob pattern, coverage information will be collected for it even if no tests exist for this file and it's never required in the test suite.

That's fine. I have JSON files laying around that end up in the "collect coverage from" set of files, however, since I set up a transform for JSON files, I was expecting the JSON files to be transformed to valid JS files. Here is my jsonTransform.js file:

module.exports = {
  process(src) {
    return src;
  },
};

But it seems that when collecting coverage data, Jest will treat my JSON files as JS files regardless of the transform. Is this a bug or is there a better solution other than adding all affected JSON files to coveragePathIgnorePatterns?

0 Answers
Related