Code coverage tells you which lines, branches, and functions your test suite actually executed — not whether your tests are good, but a useful signal about what's exercised at all. This lesson introduces the concept before the following lessons cover Karma's specific tooling.
What Code Coverage Actually Measures
Coverage tools work by instrumenting your source code — inserting invisible counters around statements, branches, and functions — before it runs. As your test suite executes, those counters record what actually ran. Afterward, the tool aggregates counters into a report showing the percentage of code touched by at least one test.
In the Karma ecosystem, this instrumentation is almost always performed by Istanbul (specifically its istanbul-lib-instrument library), the same underlying coverage engine used by Jest, Mocha, and most other JavaScript testing tools.
// Before instrumentation
function isEven(n) {
if (n % 2 === 0) {
return true;
}
return false;
}
// After instrumentation (simplified concept)
function isEven(n) {
__coverage__.f['isEven']++;
if (n % 2 === 0) {
__coverage__.b['branch1'][0]++;
return true;
}
__coverage__.b['branch1'][1]++;
return false;
}
Real instrumented code is far denser than this simplified illustration, but the underlying idea — counters injected around your logic — is exactly this.
The Four Standard Coverage Metrics
Statements: % of executable statements run at least once
Branches: % of conditional branches (if/else, ternary, &&/||) taken
Functions: % of defined functions called at least once
Lines: % of lines with executable code that ran at least once
Statement and line coverage are closely related but not identical, especially with multi-statement lines.
Branch coverage is usually the hardest metric to raise, since it requires exercising every conditional path, not just calling the surrounding function.
Function coverage catches entirely untested functions that statement coverage alone might mask if partially exercised.
100% coverage on all four metrics still doesn't guarantee correct assertions — coverage measures execution, not correctness.
Coverage Concepts Cheat Sheet
Core vocabulary before diving into Karma-specific tooling.
Term
Meaning
Instrumentation
Injecting counters into code before it runs
Istanbul
The underlying coverage engine most JS tools (including Karma) use
Statement coverage
% of executable statements run at least once
Branch coverage
% of conditional branches actually taken
Function coverage
% of functions called at least once
Coverage threshold
A minimum required percentage, often enforced in CI
Why Coverage Is a Signal, Not a Guarantee
A test that calls a function but asserts nothing meaningful still counts as 'covering' every line that function executes. High coverage numbers tell you what code ran during tests — they say nothing about whether the assertions in those tests actually verify correct behavior.
Coverage is necessary but not sufficient evidence of a well-tested codebase.
Use coverage to find completely untested code, not as a proxy for test quality.
A sudden coverage drop after a change is a more useful early-warning signal than an absolute percentage target.
Combine coverage numbers with code review of the actual assertions being made.
Where Coverage Fits Into a Karma Workflow
In Karma specifically, coverage instrumentation happens either through the dedicated karma-coverage preprocessor (covered next), or through a bundler-level plugin (like babel-plugin-istanbul inside a karma-webpack pipeline) when your project already bundles source files before serving them.
Common Mistakes
Treating a high coverage percentage as proof of a well-tested, bug-free codebase.
Chasing 100% coverage on code paths that provide little real risk reduction for the effort spent.
Ignoring branch coverage specifically while celebrating high statement/line coverage numbers.
Key Takeaways
Code coverage measures what code executed during tests, using Istanbul-based instrumentation.
The four standard metrics are statements, branches, functions, and lines.
High coverage is a useful signal but not proof of correct, meaningful test assertions.
Karma integrates coverage via karma-coverage or bundler-level instrumentation plugins.
Pro Tip
When coverage drops after a pull request, resist immediately writing tests just to push the number back up. Look first at *what specifically* became uncovered — it often points directly at genuinely untested new logic worth testing properly, rather than a number to chase blindly.
You now understand code coverage conceptually. Next, set up karma-coverage, the standard Karma coverage plugin.