Code coverage measures how much of your code is tested by automated tests. It shows which parts of the code ran during testing.
Click to reveal answer
beginner
Name three common types of code coverage.
1. Line coverage: checks if each line of code ran. 2. Branch coverage: checks if each decision path ran. 3. Function coverage: checks if each function was called.
Click to reveal answer
intermediate
Why is 100% code coverage not always necessary?
Because some code is hard to test or not critical. Also, high coverage doesn't guarantee no bugs, but it helps find many issues.
Click to reveal answer
beginner
How do you generate code coverage reports in Node.js?
You can use tools like 'nyc' (Istanbul) with your test runner. Run tests with coverage enabled to see which code ran.
Click to reveal answer
beginner
What does a coverage report typically show?
It shows percentages of lines, branches, and functions covered. It highlights uncovered code to improve tests.
Click to reveal answer
What does line coverage measure?
AIf the code compiles without errors
BIf each function was called
CIf each line of code was executed during tests
DIf each branch decision was tested
✗ Incorrect
Line coverage checks if every line of code ran during testing.
Which tool is commonly used in Node.js for code coverage?
AJest
Bnyc (Istanbul)
CMocha
DExpress
✗ Incorrect
'nyc' is a popular code coverage tool for Node.js, often used with test runners.
Why might 100% code coverage be unrealistic?
ABecause some code is unreachable or hard to test
BBecause tests are not important
CBecause coverage tools are inaccurate
DBecause code coverage slows down the app
✗ Incorrect
Some code paths are difficult or unnecessary to test, so 100% coverage is not always practical.
What does branch coverage check?
AIf each function was called
BIf the code is formatted correctly
CIf the code runs without errors
DIf each decision path (like if/else) was tested
✗ Incorrect
Branch coverage ensures all decision paths in the code are tested.
What is the main benefit of using code coverage?
ATo find untested parts of code and improve test quality
BTo make the code run faster
CTo reduce the size of the code
DTo automatically fix bugs
✗ Incorrect
Code coverage helps identify which parts of code lack tests, improving overall test quality.
Explain what code coverage is and why it is useful in testing Node.js applications.
Think about how you check if your tests run all parts of your code.
You got /4 concepts.
Describe how you would set up code coverage reporting in a Node.js project.
Consider the tools and commands you use to see which code your tests cover.
You got /4 concepts.
Practice
(1/5)
1. What does code coverage measure in a Node.js project?
easy
A. How much of the code is executed by tests
B. The speed of the Node.js server
C. The number of users accessing the app
D. The size of the project files
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 A
Quick 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
A. nyc node test.js
B. nyc mocha
C. node --coverage test.js
D. npm run coverage
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
Running nyc mocha runs mocha tests with coverage collection.
Final Answer:
nyc mocha -> Option B
Quick 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
A. 75% coverage - partial lines executed
B. 100% coverage - all lines executed
C. 0% coverage - function not called
D. 50% coverage - else branch not executed
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 D
Quick 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
A. The source files are excluded in nyc config
B. nyc was not installed
C. Mocha is not compatible with nyc
D. Tests did not run any code
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 A
Quick 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
A. Add a test calling checkValue('Yes')
B. Add a test calling checkValue(1)
C. Add a test calling checkValue(false)
D. No change needed, coverage is already 100%
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 C
Quick 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 unknownstrongwhile earning +1 Grip toward your certificate.