A Jest 30 service shows 92% line coverage yet an untested error branch shipped a bug; how do you find such branches in its Istanbul HTML report?
answer
- sort by the Branches column
- lcov-report index page
- I and E markers
- yellow means a branch
- jump with n and b
basics
~20 sOpen coverage/lcov-report/index.html, sort by the Branches column, and open the weak file. The babel provider marks an untaken if path with I and an untaken else with E, highlights other missed branches in yellow and shades unrun statements pink.
solid answer
~50 sLine coverage hides branches: a line holding a condition counts as covered once it runs, even if one outcome never happened. So read the `% Branch` column, not `% Lines`. The `lcov` reporter writes an HTML site to `coverage/lcov-report/`; its index lists each folder and file with statements, branches, functions and lines, so sort by branches and open the weakest file. In the file view, a gutter shows how many times each line ran (`2x`), unexecuted statements are shaded pink, a small `I` or `E` marks an `if` or `else` path never taken, and other untaken branches such as a ternary arm are highlighted yellow. Press `n` or `j` for the next uncovered block and `b`, `p` or `k` for the previous one. A missed `catch` body appears as uncovered statements, not as a branch.
code
plaintext · 9 linescoverage/
clover.xml
coverage-final.json
lcov.info
lcov-report/
index.html
status.js.html
block-navigation.js
base.cssgo deeper
Recall where the HTML report lives and that the Branches column, not Lines, shows conditions that only went one way.
Explain the I, E, yellow and pink markers, the hit counts in the gutter, and why a catch body is not a branch.
Show you sort by branches, compare branch and line figures to spot one-sided conditions, and check for ignore comments and missing files.
Decide which reports the team reviews and where a per-folder branch floor is worth adding after an escaped defect.
## Why the line figure hid the bug The reserved scenario: a service reports **92% line coverage**, and a bug ships from an error path no test ever took. Line coverage counts a line as covered once code on that line has run. A line such as `if (res.status >= 500) {` runs on every call, so it is covered; whether the condition was ever true is a question only the **branch** figure answers. The first habit, then, is to read `% Branch` in the text table and `Branches` in the HTML report, not just the headline line number. ## Getting to the report The default `coverageReporters` include `lcov`, which writes both `lcov.info` and an HTML site. After `jest --coverage`, open `coverage/lcov-report/index.html` in a browser (or whatever `coverageDirectory` points to). If the reporter list was customised, make sure it includes `lcov` or `html`; `lcovonly` and the text reporters do not write the site. ## Reading the index page The index lists folders, then files, each with four figures: **Statements**, **Branches**, **Functions** and **Lines**, as a percentage and as covered over total. Rows are coloured by watermarks: Istanbul's default bands are 50 and 80, and when `coverageThreshold.global` sets a metric Jest uses that value as the upper band, so a row in the top colour meets the global floor. Steps that find the gap quickly: 1. Click the Branches column header to sort ascending. 2. Look for files whose branch figure is far below their line figure. That gap is the signature of conditions that only ever went one way. 3. Open the file. ## Reading a file view The source is shown with annotations. Under the default babel provider: | What you see | Meaning | |---|---| | a count such as `2x` in the gutter | that line executed twice in the run | | a pink-shaded gutter cell | the line never executed | | pink highlight on code, tooltip "statement not covered" | that statement never ran | | a small `I` before an `if`, tooltip "if path not taken" | the condition was never true | | a small `E` before an `if`, tooltip "else path not taken" | the condition was never false, including an implicit else | | yellow highlight, tooltip "branch not covered" | another branch arm, such as one side of a ternary, never ran | | pink highlight on a function, tooltip "function not covered" | the function was never called | The page also supports the keyboard: `n` or `j` jumps to the next uncovered block, and `b`, `p` or `k` to the previous one. On a long file that is faster than scrolling. In the scenario, the culprit looks like this: an `I` in front of `if (res.status >= 500)`, with the `throw` inside it shaded pink as a statement not covered. That is the error branch no test entered. ## Cases that read differently - **try/catch.** Istanbul does not model `catch` as a branch. An unexecuted `catch` body shows as uncovered statements and lines, while `% Branch` is unaffected. Look for pink statements as well as yellow branches. - **The v8 provider.** With `coverageProvider: 'v8'`, uncovered branches are yellow ranges, and the `I` and `E` markers do not appear. Lines holding only a closing brace may show as uncovered as well. - **Ignored code.** Code excluded with `/* istanbul ignore next */` (babel provider) or `/* c8 ignore next */` (v8 provider) is left out of the figures, so check for those comments around an error path before trusting a green row. - **Files missing entirely.** A module no test imports is absent from the report unless `collectCoverageFrom` lists it, so an error branch in an untested module never appears at all. ## Turning the finding into a guard Once the untaken branch is found, the fix is a test that drives the error path and asserts what the service does there. If the folder deserves a stricter floor, a per-path `branches` entry in `coverageThreshold` keeps the figure from sliding back. The text reporter's `Uncovered Line #s` column gives the same line numbers in the terminal, which is useful when no browser is at hand.
- Why can a file show 100% lines but well under 100% branches?A line counts as covered once code on it runs, so a one-line ternary or a condition that was only ever true still covers the line. Branch coverage tracks each outcome separately, so the untaken side shows up only in the branch figure and as an `I`, `E` or yellow highlight in the file view.
- The error handling lives in a catch block, yet the branch figure is 100%. Is it tested?Not necessarily. Istanbul does not count `catch` as a branch, so an unexecuted catch body leaves `% Branch` untouched and shows up only as uncovered statements and lines. Check the statement figure and look for pink-shaded statements inside the catch.
- A high line coverage figure means every if and else path was exercised.
- An E marker in the Istanbul report means an error was thrown on that line.
- An untested catch block lowers the branch percentage.
- The HTML report needs a separate html reporter even with the default reporter set.
- The I and E markers look the same under the v8 provider.