Skip to content

coverage-v8: Text reporter counts ~2x branches of JSON coverage file for nested ternaries (React/TypeScript) #10394

@lautou

Description

@lautou

Description

When using @vitest/coverage-v8 in a React/TypeScript project, the text reporter counts approximately 2× more branches than what is reported in coverage-final.json for the same codebase. This causes the text reporter to show ~94–95% branch coverage while the JSON canonical file shows 100% branch coverage.

Environment

  • Vitest: 4.1.6
  • @vitest/coverage-v8: 4.1.6
  • ast-v8-to-istanbul: 1.0.0 (latest — already includes fix from PR Setup Tests Global Describe? #138)
  • Node.js: 22.x
  • Framework: React 18 + TypeScript
  • Not Vue SFC — plain React/TypeScript project

Reproduction

The discrepancy appears on nested ternary chains such as:

const normalizedQty = isCashDirectDeposit
  ? (direction === 'depot' ? Math.abs(form.quantity) : -Math.abs(form.quantity))
  : form.type === 'Frais'
    ? forfait ? -1 : -Math.abs(form.quantity)
    : form.type === 'Revenu'
    ? Math.abs(form.quantity)
    : form.quantity;

Observed behavior

Text reporter output:

All files | % Stmts | % Branch | % Funcs | % Lines
          |   100   |  94.79   |   100   |   100

Branches: 94.79% ( 1348/1424 )

JSON coverage (coverage-final.json) analysis:

Total uncovered branches: 0/689 (100.00%)

The text reporter counts 1424 branches, while the JSON counts 689 — roughly 2×.

All 689 branches are covered (0 uncovered in JSON). The ~735 extra "branches" in the text reporter appear to come from nested ternary expressions being counted multiple times.

Expected behavior

Text reporter branch count should match the JSON coverage file count, or at minimum produce the same covered/total ratio.

Additional notes

Workaround

Currently setting the coverage threshold to 94% instead of 100% to accommodate the false count. The JSON canonical output confirms true 100% coverage.

Metadata

Metadata

Assignees

No one assigned

    Labels

    maybe automatedUser is likely an AI agent, or the content was generated by an AI assistant without user controlneeds reproduction

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions