Skip to content

Line Coverage

In one line

Line coverage is the simplest coverage metric — what percentage of your code lines were executed by tests.

In simple words

Line coverage is the most basic coverage metric. It simply counts how many lines of code were executed during your test run, divided by the total number of lines.

For example, if your file has 100 lines of code and your tests execute 85 of them, your line coverage is 85%.

Line coverage is easy to understand but can be misleading. A line can be 'covered' (executed) without being properly tested. For example, if a line runs but you never assert on its output, it's covered but not verified. Also, a single line with a ternary operator might have both branches but line coverage only counts it as one line.

Use line coverage as a starting point, but also look at branch and function coverage for a more complete picture.

Example

javascript
// Source file: math.js
function abs(n) {
  if (n < 0) return -n;   // line 2 — covered when n < 0
  return n;               // line 3 — covered when n >= 0
}

// Test file: math.test.js
test('abs of negative', () => {
  expect(abs(-5)).toBe(5);  // covers line 2 only
});

// Coverage report:
// File     | % Stmts | % Branch | % Lines |
// math.js  |   75%   |   50%    |  100%   |
// Line 3 is covered (return n) but the n >= 0 branch is not tested explicitly.

Line coverage tracks how many source lines execute during tests. Here the test covers line 2 (n < 0 path); line 3 runs as a fallback but the positive-input branch is not explicitly tested, leaving branch coverage at 50%.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary