Skip to content

fix: do not let --all overwrite coverage of files that were loaded - #1622

Open
Eljees wants to merge 1 commit into
istanbuljs:mainfrom
Eljees:fix/all-must-not-overwrite-real-coverage-1372
Open

fix: do not let --all overwrite coverage of files that were loaded#1622
Eljees wants to merge 1 commit into
istanbuljs:mainfrom
Eljees:fix/all-must-not-overwrite-real-coverage-1372

Conversation

@Eljees

@Eljees Eljees commented Jul 30, 2026

Copy link
Copy Markdown

Fixes #1372

--all can wipe out the coverage of files that were exercised, which is why the report comes back at
0% for everything rather than just for the untouched files.

addAllFiles() writes its placeholder straight into the global coverage object, unconditionally:

const coverage = coverageFinder()
const lastCoverage = this.instrumenter().lastFileCoverage()
if (lastCoverage) {
  coverage[lastCoverage.path] = {
    ...lastCoverage,
    // Only use this data if we don't have it without `all: true`
    all: true
  }
}

The comment already states the intended rule — only use this data if we don't have it without
all: true
— but there is no condition in the code implementing it. When a file already has real
counters in coverage[path], the freshly instrumented (all-zero) copy replaces them.

The fix is the missing check. Everything else stays as it was: files that were never loaded still get
their zeroed placeholder.

Before / after

New case in test/add-all-files.js — real counters are put in the global coverage object first, then
addAllFiles() runs, and the test asserts they survived:

before:  not ok - real hit count survives --all
         not ok - entry is not marked as an --all placeholder
         # { total: 15, pass: 13, fail: 2 }

after:   ok - real hit count survives --all
         ok - entry is not marked as an --all placeholder
         # { total: 15, pass: 15 }

Run with tap test/add-all-files.js after npm run instrument, on node 24.

I kept the wording of the original comment and only moved it above the if, since it now describes a
condition that actually exists.

AI-assisted (LLM used for drafting); the change and both runs above are mine.

addAllFiles wrote its zeroed placeholder into the global coverage object unconditionally, replacing real counters for files that had been exercised. The existing comment already described the intended condition; this adds it.

Signed-off-by: Eljees <3.14hell@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

nyc --all option reporting wrong coverage results if all files are instrumented

1 participant