Skip to article content
FrontendXLesson 14 / 24

Coverage and Honest Quality Signals

Build a working model of automated testing from assertions to unit, component, API, integration, contract and end-to-end tests, then apply it to a single progressive task-board capstone where each milestone adds a real defect, a failing test, a corrected implementation and an expected result.

intermediate · about 35 minutes · 6 exercises

  • Predict
  • Explain
  • Implement
  • Debug
  • Test
  • Interview

What you will be able to do

#
  • Distinguish statement, branch and path coverage.
  • Explain what a coverage threshold does and does not guarantee.
  • Choose quality signals that match the risk of a codebase.

Overview

#

Three kinds of coverage

Statement coverage counts executed lines. Branch coverage counts executed branches of each conditional, so an if with only the true path taken reports partial branch coverage. Path coverage counts combinations of branches, which grows exponentially and is rarely achieved. Branch coverage is the most useful of the three because untested branches are where defects hide.

snippet.js
function classify(n) {
  if (n < 0) return 'negative';
  if (n === 0) return 'zero';
  return 'positive';
}

Calling classify(5) gives full statement coverage but leaves the negative and zero branches untested. Branch coverage would flag them.

A threshold is a floor, not a goal

A coverage threshold prevents coverage from dropping. It does not prove the covered lines are asserted. A team can hit 90 percent coverage with tests that assert nothing. The threshold is a guardrail against neglect, not a measure of quality.

Coverage as a debugging tool

Coverage is most useful in reverse: open the report, find the uncovered branches, and ask whether each one is untested because it is unreachable or because it was forgotten. The first answer may mean dead code. The second answer is a missing test. Using coverage to find gaps is valuable; using it as a leaderboard is not.

Signals that match the risk

Different codebases need different signals. A payments module benefits from branch coverage, mutation testing and property-based tests. A marketing page benefits from a smoke test that it renders. A library benefits from a public API contract test. Choose signals from the cost of a defect, not from a template.

Do not game the number

When a threshold becomes a target, people write tests that call code without asserting, or add istanbul ignore comments. Both raise the number and lower the quality. If the number is being gamed, the metric is being used wrong.

Report coverage per change

A more useful practice than a global threshold is to require that new and changed lines are covered. This keeps the suite growing with the code without demanding a rewrite of legacy coverage.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    Only `classify(5)` is tested. Which branches are uncovered?

    Read the code, then choose the best answer. The reference explanation stays hidden until you respond.
    stimulus.javascript
    function classify(n) {
      if (n < 0) return 'negative';
      if (n === 0) return 'zero';
      return 'positive';
    }
    console.log(classify(5));
    Choose the best answer
  2. Exercise 2Explain

    Explain why a high coverage threshold can coexist with weak tests.

    Reveal the reference answer
    reference.javascript
    Coverage counts execution, not assertions. A test that calls a function and checks nothing still marks its lines covered. So a suite can reach a high threshold with tests that would pass even if the covered code were wrong. Coverage measures how much code the tests touch, not how strongly they check it. Mutation testing and deliberate review are needed to measure strength.

    The answer should separate touching code from checking code and point to mutation testing as the strength measure.

  3. Exercise 3Implement

    Implement `branchCoverage(taken, total)` returning the percentage rounded down to an integer, or 100 when total is 0. `console.log(branchCoverage(3, 4), branchCoverage(0, 0))` should print `75 100`.

    starter.javascript
    function branchCoverage(taken, total) {
      // your code here
    }

    Reveal the reference answer
    reference.javascript
    function branchCoverage(taken, total) {
      if (total === 0) return 100;
      return Math.floor((taken / total) * 100);
    }

    Authored expected output: 75 100

    Floor rounding avoids overstating coverage. The total-zero guard returns 100 because a module with no branches is trivially fully covered, and it avoids division by zero.

  4. Exercise 4Debug

    This test raises coverage but would pass even if `applyTax` returned the input unchanged. Fix it to actually check the behavior.

    starter.javascript
    function applyTax(price, rate) {
      return price * (1 + rate);
    }
    applyTax(100, 0.2);
    console.log('covered');

    Reveal the reference answer
    reference.javascript
    function applyTax(price, rate) {
      return price * (1 + rate);
    }
    const result = applyTax(100, 0.2);
    if (result === 120) {
      console.log('pass');
    } else {
      throw new Error('expected 120 but got ' + result);
    }

    Authored expected output: pass

    Calling the function with no assertion covers the line but checks nothing. Asserting the exact result 120 fails if the tax is not applied, so the test now has strength as well as coverage.

  5. Exercise 5Test

    Write tests that achieve full branch coverage of `classify`.

    starter.javascript
    function classify(n) {
      if (n < 0) return 'negative';
      if (n === 0) return 'zero';
      return 'positive';
    }

    Reveal the reference answer
    reference.javascript
    function assertEqual(actual, expected, label) {
      if (actual !== expected) throw new Error(label + ': expected ' + expected + ' but got ' + actual);
    }
    assertEqual(classify(-1), 'negative', 'negative branch');
    assertEqual(classify(0), 'zero', 'zero branch');
    assertEqual(classify(1), 'positive', 'positive branch');
    console.log('classify branches covered');

    Authored expected output: classify branches covered

    One value per branch exercises every true and false path. The assertions also check the returned string, so the coverage is backed by real checks rather than bare calls.

  6. Exercise 6Interview

    In an interview, explain how you would set a coverage policy for a team.

    Reveal the reference answer
    reference.javascript
    I would require that new and changed lines are covered, and set a modest global floor to prevent regressions, rather than demanding a high number everywhere. I would use branch coverage because untested branches hide defects. I would pair coverage with mutation testing on the highest-risk modules and treat the numbers as diagnostics. I would explicitly forbid ignore comments used only to inflate the number.

    The answer should favor per-change coverage, branch coverage, and pairing with mutation testing, and should name the gaming risk.

Common misconceptions

#
  • Statement coverage equals branch coverage.
  • A 100 percent coverage threshold guarantees correct code.
  • Coverage should be the primary quality metric.

Follow-up practice

#
  • testing-mutation
  • testing-capstone-quality-task-board

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.