0
0
Node.jsframework~10 mins

Code coverage basics in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Code coverage basics
Write code to test
Write tests for code
Run tests with coverage tool
Coverage tool tracks executed lines
Generate coverage report
Review report to find untested code
Improve tests to cover missing parts
Back to Run tests
This flow shows how code coverage works: write code, test it, run coverage, see report, then improve tests.
Execution Sample
Node.js
function sum(a, b) {
  if (a && b) {
    return a + b;
  }
  return 0;
}

// Test calls sum(1, 2) and sum(0, 2)
This code defines a sum function with a condition and tests it with two calls to check coverage.
Execution Table
StepCode LineExecuted?ReasonCoverage Status
1function sum(a, b) {YesFunction declaredCovered
2 if (a && b) {YesTest sum(1, 2) runs this true branchCovered
3 return a + b;Yessum(1, 2) returns 3Covered
4 }YesEnd if blockCovered
5 return 0;YesTest sum(0, 2) runs this else branchCovered
6}YesFunction endsCovered
7// Test calls sum(1, 2) and sum(0, 2)N/AComments not countedN/A
💡 All code lines executed by tests, coverage is 100%
Variable Tracker
VariableStartAfter sum(1, 2)After sum(0, 2)
aundefined10
bundefined22
return valuenone30
Key Moments - 2 Insights
Why does the coverage tool mark line 5 as covered even though it looks like an else?
Because the test sum(0, 2) calls the function with a=0, b=2, which makes the if condition false, so line 5 runs. See execution_table row 5.
Are comments counted in code coverage?
No, comments are ignored by coverage tools. See execution_table row 7 marked N/A.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which line runs only when both a and b are truthy?
ALine 3 (return a + b)
BLine 5 (return 0)
CLine 2 (if condition)
DLine 1 (function declaration)
💡 Hint
Check rows 2 and 3 in execution_table where sum(1, 2) runs the true branch.
At which step does the coverage tool detect the else branch is executed?
AStep 2
BStep 3
CStep 5
DStep 1
💡 Hint
Look at execution_table row 5 where return 0 runs for sum(0, 2).
If we remove the test sum(0, 2), what happens to coverage on line 5?
ALine 5 remains covered
BLine 5 becomes uncovered
CLine 5 is ignored
DLine 5 coverage increases
💡 Hint
Refer to variable_tracker and execution_table rows showing line 5 runs only when sum(0, 2) is called.
Concept Snapshot
Code coverage tracks which lines run during tests.
Run tests with coverage tool to see report.
Report shows covered and uncovered code.
Improve tests to cover missing lines.
Helps find untested parts of code.
Full Transcript
Code coverage basics means checking which parts of your code run when you run tests. You write your code and tests, then run a coverage tool that tracks which lines execute. It creates a report showing covered and uncovered lines. For example, a sum function with an if condition can be tested with different inputs to cover both branches. Coverage helps you find code not tested so you can add tests and improve quality.