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 import
ed JSON files.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The default
extension
fortest-exclude
does not include.json
so I’m confused about how this is happening (it doesn’t look like you setextension
when constructing the test-exclude object).@coreyfarrell interesting, I’ll need to add a unit test, maybe
test-exclude
isn’t the culprit.