diff --git a/lib/report.js b/lib/report.js index 1fa51dfc..fd68c7e7 100644 --- a/lib/report.js +++ b/lib/report.js @@ -487,7 +487,6 @@ class Report { if (/^file:\/\//.test(v8ScriptCov.url)) { try { v8ScriptCov.url = fileURLToPath(v8ScriptCov.url) - fileIndex.add(v8ScriptCov.url) } catch (err) { debuglog(`${err.stack}`) continue @@ -496,6 +495,7 @@ class Report { if ((!this.omitRelative || isAbsolute(v8ScriptCov.url))) { if (this.excludeAfterRemap || this._shouldInstrument(v8ScriptCov.url)) { result.push(v8ScriptCov) + fileIndex.add(v8ScriptCov.url) } } } diff --git a/test/integration.js b/test/integration.js index 8e00be0b..07112731 100644 --- a/test/integration.js +++ b/test/integration.js @@ -5,6 +5,8 @@ const { resolve } = require('path') const { spawnSync } = require('child_process') const { statSync, rm } = require('fs') const { dirname } = require('path') +const { pathToFileURL } = require('url') +const createReport = require('../lib/report') const c8Path = require.resolve('../bin/c8') const nodePath = process.execPath const tsNodePath = './node_modules/.bin/ts-node' @@ -592,6 +594,43 @@ beforeEach(function () { ]) output.toString('utf8').should.matchSnapshot() }) + + it('should only track files that pass shouldInstrument in fileIndex', () => { + const report = createReport({ + include: ['fake/src/**/*.js'], + exclude: [], + tempDirectory: 'tmp/filtered-fileindex', + reportsDirectory: 'coverage/filtered-fileindex', + reporter: ['text'] + }) + + const includedFile = resolve('fake/src/should-be-in-fileindex.js') + const excludedFile = resolve('fake/lib/should-not-be-in-fileindex.js') + const entryFile = resolve('fake/should-not-be-in-fileindex.js') + + const fileIndex = new Set() + const fakeCovEntry = (scriptId, filepath) => ({ + scriptId, + url: pathToFileURL(filepath).href, + functions: [{ + functionName: '', + ranges: [{ startOffset: 0, endOffset: 100, count: 1 }], + isBlockCoverage: true + }] + }) + + report._normalizeProcessCov({ + result: [ + fakeCovEntry('1', includedFile), + fakeCovEntry('2', excludedFile), + fakeCovEntry('3', entryFile) + ] + }, fileIndex) + + fileIndex.has(includedFile).should.equal(true) + fileIndex.has(excludedFile).should.equal(false) + fileIndex.has(entryFile).should.equal(false) + }) }) // see: https://github.com/bcoe/c8/issues/149 it('cobertura report escapes special characters', () => {