Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,12 @@ class NYC {
}
const coverage = coverageFinder()
const lastCoverage = this.instrumenter().lastFileCoverage()
if (lastCoverage) {
// Only use this data if we don't have it without `all: true`: a file that
// was actually exercised already has real counters here, and overwriting
// them with the empty placeholder would report it as uncovered.
if (lastCoverage && !coverage[lastCoverage.path]) {
coverage[lastCoverage.path] = {
...lastCoverage,
// Only use this data if we don't have it without `all: true`
all: true
}
}
Expand Down
27 changes: 27 additions & 0 deletions test/add-all-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,33 @@ t.test('outputs an empty coverage report for all files that are not excluded', a
t.equal(report.s['1'], 0)
})

t.test('does not overwrite the coverage of a file that was actually loaded', async t => {
const nyc = new NYC(await parseArgv(fixtures))
await nyc.reset()

// Pretend the file was exercised before `--all` walks the tree: real counters
// are already sitting in the global coverage object.
const loadedPath = path.join(fixtures, './not-loaded.js')
global.__coverage__ = global.__coverage__ || {}
global.__coverage__[loadedPath] = {
path: loadedPath,
statementMap: { 0: { start: { line: 1, column: 0 }, end: { line: 1, column: 1 } } },
fnMap: {},
branchMap: {},
s: { 0: 7 },
f: {},
b: {}
}

await nyc.addAllFiles()

const reports = (await nyc.coverageData()).filter(report => ap(report)[loadedPath])
const report = reports[0][loadedPath]

t.equal(report.s['0'], 7, 'real hit count survives --all')
t.notOk(report.all, 'entry is not marked as an --all placeholder')
})

t.test('outputs an empty coverage report for multiple configured extensions', async t => {
const cwd = path.resolve(fixtures, './conf-multiple-extensions')
const nyc = new NYC(await parseArgv(cwd))
Expand Down