question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Import of package.json causes package.json file to be included in coverage report and fail

See original GitHub issue
  • Version: v12.10.0
  • Platform: Windows 10 Pro version 1803

Was updating a package to change a createRequire style import of package.json into the now-supported direct import of the package file. In doing so, the package.json file is now showing up in coverage reports and failing on uncovered function.

Original working code

import { createRequire } from 'module';

const require = createRequire(import.meta.url);
const pkg = require('../package.json');

Here is code coverage output for this case:

  🌈 SUMMARY RESULTS 🌈  
                         

Suites:   3 passed, 3 of 3 completed
Asserts:  50 passed, of 50
Time:     16s
----------------------|----------|----------|----------|----------|-------------------|
File                  |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------------------|----------|----------|----------|----------|-------------------|
All files             |      100 |      100 |      100 |      100 |                   |
 package-composer     |      100 |      100 |      100 |      100 |                   |
  index.mjs           |      100 |      100 |      100 |      100 |                   |
 package-composer/bin |      100 |      100 |      100 |      100 |                   |
  index.mjs           |      100 |      100 |      100 |      100 |                   |
 package-composer/lib |      100 |      100 |      100 |      100 |                   |
  config.mjs          |      100 |      100 |      100 |      100 |                   |
  licenses.mjs        |      100 |      100 |      100 |      100 |                   |
  questions.mjs       |      100 |      100 |      100 |      100 |                   |
  utils.mjs           |      100 |      100 |      100 |      100 |                   |
----------------------|----------|----------|----------|----------|-------------------|
info: Finished 'executeTests' after 21s

Failing code

import pkg from '../package.json';

and failing coverage output


                         
  🌈 SUMMARY RESULTS 🌈  
                         

Suites:   3 passed, 3 of 3 completed
Asserts:  50 passed, of 50
Time:     16s
----------------------|----------|----------|----------|----------|-------------------|
File                  |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------------------|----------|----------|----------|----------|-------------------|
All files             |      100 |      100 |    97.22 |      100 |                   |
 package-composer     |      100 |      100 |       75 |      100 |                   |
  index.mjs           |      100 |      100 |      100 |      100 |                   |
  package.json        |      100 |      100 |       50 |      100 |                   |
 package-composer/bin |      100 |      100 |      100 |      100 |                   |
  index.mjs           |      100 |      100 |      100 |      100 |                   |
 package-composer/lib |      100 |      100 |      100 |      100 |                   |
  config.mjs          |      100 |      100 |      100 |      100 |                   |
  licenses.mjs        |      100 |      100 |      100 |      100 |                   |
  questions.mjs       |      100 |      100 |      100 |      100 |                   |
  utils.mjs           |      100 |      100 |      100 |      100 |                   |
----------------------|----------|----------|----------|----------|-------------------|
ERROR: Coverage for functions (97.22%) does not meet global threshold (100%)

Unfortunately, without any sort of way to ignore package.json file (via config or via ignore comment in JSON file), this pretty much prevents use of this newer JSON import feature in Node. if we want to meet 100% coverage.

I have not tested with other imported JSON files.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
coreyfarrellcommented, Mar 8, 2020

The default extension for test-exclude does not include .json so I’m confused about how this is happening (it doesn’t look like you set extension when constructing the test-exclude object).

0reactions
bcoecommented, Mar 8, 2020

@coreyfarrell interesting, I’ll need to add a unit test, maybe test-exclude isn’t the culprit.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Require package.json within environment.ts causes error #2396
Require package.json within environment.ts causes error We're getting the following error: Uncaught SyntaxError: Unexpected token ':' at ...
Read more >
Jest passing tests but --covering option not picking up files
This way I exclude a number of files I do not want to count coverage from, such as my modules, mocks, and tests....
Read more >
Configuring package.json · Jest
Indicates whether the coverage information should be collected while executing the test. Because this retrofits all executed files with coverage collection ...
Read more >
package.json - npm Docs
If a dependency can be used, but you would like npm to proceed if it cannot be found or fails to install, then...
Read more >
Configuring Jest
For example, you may hide coverage report lines for all fully-covered files: JavaScript; TypeScript. /** @type {import('jest').Config} */
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found