In this blog, we’ll dive into what NYC code coverage is, why it matters, how to use it in your project, and best practices to make the most of it.
What is NYC?
NYC is a command-line utility for measuring code coverage in Node.js applications. It’s built on top of Istanbul, a popular JavaScript instrumentation library. NYC captures how much of your code is executed when running tests and generates reports to help you identify untested logic.
Key Features of NYC:
- Supports ES6, CommonJS, and TypeScript
- Integrates with popular test frameworks (Mocha, Jest, AVA)
- Generates reports in various formats (HTML, lcov, text)
- Easily configurable via command-line or config file
Why Code Coverage Matters
Code coverage shows the percentage of your codebase that is executed during test runs. This gives you insights into:
- Test Effectiveness: Are your tests actually exercising the code?
- Uncovered Logic: Are there paths in your code that aren’t being tested?
- Confidence in Changes: High coverage means a higher chance of catching regressions.
However, coverage is not quality. You can have 100% coverage and still miss edge cases. Use it as a guide—not a guarantee.
Installing NYC
You can add NYC to your project in a few simple steps:
bash
CopyEdit
npm install --save-dev nyc
If you haven’t already, install a testing framework like Mocha:
bash
CopyEdit
npm install --save-dev mocha
Basic Usage with Mocha
Assuming you have a test file in test/app.test.js and a source file in src/app.js, you can run:
bash
CopyEdit
nyc mocha
NYC will instrument your code, run the tests, and show a coverage summary in the terminal.
Example output:
yaml
CopyEdit
Statements : 85.71% ( 6/7 )
Branches : 75% ( 3/4 )
Functions : 100% ( 2/2 )
Lines : 85.71% ( 6/7 )
Configuring NYC
You can add NYC configuration in your package.json or a separate .nycrc file.
Example (in package.json):
json
CopyEdit
"nyc": {
"reporter": ["text", "html"],
"include": ["src/**/*.js"],
"exclude": ["test/**/*.js"],
"all": true
}
Key Options:
- reporter: Choose from text, html, lcov, cobertura, etc.
- include: Files or directories to include
- exclude: What to skip (e.g., test files, configs)
- all: Whether to instrument all files, even if not required by tests
Generating HTML Reports
To generate an interactive report:
bash
CopyEdit
nyc mocha
nyc report --reporter=html
Open the report in your browser:
bash
CopyEdit
open coverage/index.html
The HTML report allows you to drill down into files and see exactly which lines are covered or missed.
Integrating with CI/CD
To enforce a minimum coverage threshold in your CI pipeline, add:
bash
CopyEdit
nyc --check-coverage --lines 80 --functions 80 --branches 70 --statements 80 mocha
If the thresholds aren’t met, the command will fail, preventing bad code from being merged.
Tips & Best Practices
1. Aim for Meaningful Coverage
Don’t chase 100% just for the number. Focus on critical paths, business logic, and edge cases.
2. Use all: true
Without all: true, NYC will only report coverage on files that were required during the test run, potentially missing unused code.
3. Exclude Non-Code Files
Use .nycrc or package.json config to exclude files like config files, mocks, or generated code.
4. Run Locally and in CI
Integrate NYC into your development workflow and CI pipeline to maintain consistent quality checks.
5. Pair with Linting and Type Checks
High coverage doesn’t mean high quality. Use tools like ESLint and TypeScript alongside NYC for well-rounded code quality assurance.
Alternatives and Complementary Tools
- Jest: Has built-in code coverage using Istanbul, so you don’t need NYC separately.
- C8: A lighter alternative that uses V8's built-in coverage.
- Keploy: Automatically generates test cases from API traffic, helping you boost test coverage without writing tests manually.
Final Thoughts
NYC makes it easy to add visibility into your test suite’s effectiveness. While coverage doesn’t guarantee bug-free code, it’s a powerful metric for guiding better testing decisions and catching silent failures.
Start small—instrument your code, track reports, and gradually improve coverage where it matters most. Over time, NYC will become a cornerstone of your quality assurance toolkit in any serious JavaScript project.
Read more on- https://keploy.io/blog/technology/how-to-handle-node-js-code-coverage-with-nyc-in-docker-containers