Skip to content

Commit 9113449

Browse files
committed
fix: Fix usage of excludeAfterRemap not to set the coverage always to 100
Do not use the `excludePath` callback. Remove the excluded sources at the end, after all coverage parts have been merged. This might be a problem in `v8-to-istanbul`, because `istanbul-lib-coverage` doesn't offer a method to filter the coverage data at the end. Attempts to fix bcoe#462
1 parent 2f36fe9 commit 9113449

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

lib/report.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,14 @@ class Report {
9898
try {
9999
const sources = this._getSourceMap(v8ScriptCov)
100100
const path = resolve(this.resolve, v8ScriptCov.url)
101-
const converter = v8toIstanbul(path, this.wrapperLength, sources, (path) => {
101+
const converter = v8toIstanbul(path, this.wrapperLength, sources
102+
// v8toIstanbul sets all coverage stats of the remaining files
103+
// to 100, if some files are excluded here. See below.
104+
/*, (path) => {
102105
if (this.excludeAfterRemap) {
103106
return !this._shouldInstrument(path)
104107
}
105-
})
108+
} */)
106109
await converter.load()
107110

108111
if (resultCountPerPath.has(path)) {
@@ -131,6 +134,20 @@ class Report {
131134
map.merge(converter.toIstanbul())
132135
}
133136
}
137+
138+
// Workaround for too early applied excludeAfterRemap above -
139+
// v8toIstanbul sets all coverage stats of the remaining files
140+
// to 100, if some files are excluded by the excludePath callback.
141+
if (this.excludeAfterRemap) {
142+
const { data } = map
143+
map.data = Object.keys(data).reduce((result, key) => {
144+
if (this._shouldInstrument(key)) {
145+
result[key] = data[key]
146+
}
147+
return result
148+
}, {})
149+
}
150+
134151
this._allCoverageFiles = map
135152
return this._allCoverageFiles
136153
}

test/integration.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ hey
156156
---------------------------------------|---------|----------|---------|---------|-------------------
157157
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
158158
---------------------------------------|---------|----------|---------|---------|-------------------
159-
All files | 1.91 | 12 | 6.25 | 1.91 |
159+
All files | 1.89 | 12 | 6.25 | 1.89 |
160160
c8 | 0 | 0 | 0 | 0 |
161161
index.js | 0 | 0 | 0 | 0 | 1
162162
c8/bin | 0 | 0 | 0 | 0 |
@@ -168,7 +168,7 @@ All files | 1.91 | 12 | 6.25 | 1.91
168168
c8/lib | 0 | 0 | 0 | 0 |
169169
is-cjs-esm-bridge.js | 0 | 0 | 0 | 0 | 1-10
170170
parse-args.js | 0 | 0 | 0 | 0 | 1-218
171-
report.js | 0 | 0 | 0 | 0 | 1-340
171+
report.js | 0 | 0 | 0 | 0 | 1-357
172172
source-map-from-file.js | 0 | 0 | 0 | 0 | 1-100
173173
c8/lib/commands | 0 | 0 | 0 | 0 |
174174
check-coverage.js | 0 | 0 | 0 | 0 | 1-70

0 commit comments

Comments
 (0)