Code coverage shows how much of your code is tested by your tests. It helps find parts of code that need more testing.
Code coverage basics in Node.js
Introduction
Syntax
Node.js
npx nyc node your_test_file.js # or with npm scripts "scripts": { "test": "nyc mocha" }
nyc is a popular tool to measure code coverage in Node.js.
You run your tests with nyc to collect coverage data automatically.
Examples
nyc.Node.js
npx nyc node test.js
package.json to run tests with coverage.Node.js
"scripts": { "test": "nyc mocha" }
Node.js
npx nyc --reporter=html mocha
Sample Program
This simple Node.js script has two functions and tests for both. Running it with nyc will show coverage for add and multiply.
Node.js
const assert = require('assert'); function add(a, b) { return a + b; } function multiply(a, b) { return a * b; } // Simple tests assert.strictEqual(add(2, 3), 5); assert.strictEqual(multiply(2, 3), 6); console.log('Tests passed');
Important Notes
Code coverage does not guarantee your code is bug-free, only that tests run those parts.
Try to cover important logic and edge cases in your tests for better quality.
Use coverage reports to find untested code and add tests there.
Summary
Code coverage measures how much code your tests run.
Use tools like nyc to collect coverage in Node.js projects.
Coverage helps improve test quality and find missing tests.
Practice
1. What does code coverage measure in a Node.js project?
easy
Solution
Step 1: Understand the purpose of code coverage
Code coverage shows which parts of your code are run when tests execute.Step 2: Identify what code coverage measures
It measures the percentage of code lines or branches tested, not speed or size.Final Answer:
How much of the code is executed by tests -> Option AQuick Check:
Code coverage = executed code percentage [OK]
Hint: Code coverage = tested code parts percentage [OK]
Common Mistakes:
- Confusing coverage with performance
- Thinking coverage measures user count
- Assuming coverage measures file size
2. Which command is commonly used to run tests with coverage in a Node.js project using nyc?
easy
Solution
Step 1: Recall nyc usage with test runners
nyc is a tool that wraps test commands like mocha to collect coverage.Step 2: Identify correct syntax
Runningnyc mocharuns mocha tests with coverage collection.Final Answer:
nyc mocha -> Option BQuick Check:
nyc + test runner = coverage run [OK]
Hint: Use nyc before test command to collect coverage [OK]
Common Mistakes:
- Using nyc without a test runner
- Trying node --coverage (not standard)
- Assuming npm run coverage always exists
3. Given this code and test, what coverage report will nyc show for the function
add?
function add(a, b) {
if (a > 0) {
return a + b;
} else {
return 0;
}
}
// Test
add(2, 3);medium
Solution
Step 1: Analyze which branches run
The test calls add(2, 3), so the if condition (a > 0) is true, running the first return.Step 2: Identify uncovered code
The else branch is never executed, so that part is not covered.Final Answer:
50% coverage - else branch not executed -> Option DQuick Check:
Uncovered else branch = 50% coverage [OK]
Hint: Check if all branches run to get full coverage [OK]
Common Mistakes:
- Assuming all code runs if function is called once
- Ignoring branches in coverage
- Confusing line coverage with function call
4. You ran
nyc mocha but the coverage report shows 0% coverage. What is the most likely cause?medium
Solution
Step 1: Understand why coverage can be zero
If source files are excluded in nyc config, coverage won't track them.Step 2: Check other options
Tests running but no coverage means exclusion is likely; nyc installed and mocha compatible.Final Answer:
The source files are excluded in nyc config -> Option AQuick Check:
Excluded files cause zero coverage [OK]
Hint: Check nyc config excludes before blaming tests [OK]
Common Mistakes:
- Assuming tests never ran
- Ignoring nyc config exclusions
- Thinking nyc and mocha are incompatible
5. You want to improve coverage for this function:
function checkValue(x) {
if (x) {
return 'Yes';
} else {
return 'No';
}
}
Your tests only call checkValue(true). What change will increase coverage?hard
Solution
Step 1: Identify which branches are tested
Calling checkValue(true) runs the if branch only, not the else branch.Step 2: Determine how to cover else branch
Passing false triggers the else branch, increasing coverage.Final Answer:
Add a test calling checkValue(false) -> Option CQuick Check:
Test both true and false for full branch coverage [OK]
Hint: Test both true and false to cover all branches [OK]
Common Mistakes:
- Testing other truthy values
- Assuming one test covers all branches
- Ignoring falsy values in coverage
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
