Skip to content

Commit

Permalink
fix: files with no coverage should be preserved by default (#54)
Browse files Browse the repository at this point in the history
Closes #53
  • Loading branch information
lqc authored and mattlewis92 committed Aug 24, 2018
1 parent c484c1a commit e5a5545
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module.exports = function(config) {
// if using webpack and pre-loaders, work around webpack breaking the source path
fixWebpackSourcePaths: true,

// stop istanbul outputting messages like `File [${filename}] ignored, nothing could be mapped`
// Omit files with no statements, no functions and no branches from the report
skipFilesWithNoCoverage: true,

// Most reporters accept additional config options. You can pass these through the `report-config` option
Expand Down
9 changes: 9 additions & 0 deletions src/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ function CoverageIstanbulReporter(baseReporterDecorator, logger, config) {
const remappedCoverageMap = sourceMapStore.transformCoverage(coverageMap)
.map;

if (!coverageConfig.skipFilesWithNoCoverage) {
coverageMap.files().forEach(path => {
if (!(path in remappedCoverageMap)) {
// Re-add empty coverage record
remappedCoverageMap.addFileCoverage(path);
}
});
}

log.debug('Writing coverage reports:', reportTypes);
reporter.write(remappedCoverageMap);

Expand Down
65 changes: 48 additions & 17 deletions test/reporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { OUTPUT_LOG_FILE } = require('./karma.conf');
const { expect } = chai;
const OUTPUT_PATH = path.join(__dirname, 'fixtures', 'outputs');
const OUTPUT_FILE = path.join(OUTPUT_PATH, 'coverage-summary.json');
const OUTPUT_DETAILS_FILE = path.join(OUTPUT_PATH, 'coverage-final.json');
const fileReadTimeout = 300;

function createServer(config) {
Expand Down Expand Up @@ -164,33 +165,63 @@ describe('karma-coverage-istanbul-reporter', () => {
});
});

it('should not map files with no coverage', done => {
const server = createServer({
describe('skipFilesWithNoCoverage', () => {
const createConfig = skipFilesWithNoCoverage => ({
files: ['fixtures/typescript/src/ignored-file.ts'],
preprocessors: {
'fixtures/typescript/src/ignored-file.ts': ['webpack', 'sourcemap']
},
logLevel: 'DEBUG',
coverageIstanbulReporter: {
reports: ['json-summary'],
reports: ['json'],
dir: path.join(__dirname, 'fixtures', 'outputs'),
skipFilesWithNoCoverage: true
skipFilesWithNoCoverage
}
});
server.start();
server.on('run_complete', () => {
setTimeout(() => {
// Hacky workaround to make sure the file has been written
const output = fs.readFileSync(OUTPUT_LOG_FILE).toString();
expect(
Boolean(
output.match(
/\[DEBUG\] reporter\.coverage-istanbul - File \[\/.+test\/fixtures\/typescript\/src\/ignored-file\.ts\] ignored, nothing could be mapped/
it('should map files with no coverage', done => {
const server = createServer(createConfig(false));
server.start();
server.on('run_complete', () => {
setTimeout(() => {
const data = JSON.parse(fs.readFileSync(OUTPUT_DETAILS_FILE));

// Extract coverage data for ignored-file.ts
const keys = Object.keys(data);
expect(keys).to.have.lengthOf(
1,
'Expected data for "ignored-file.ts"'
);
const fileCoverage = data[keys[0]];

// All maps should be empty
expect(fileCoverage.statementMap).to.deep.equal({});
expect(fileCoverage.fnMap).to.deep.equal({});
expect(fileCoverage.branchMap).to.deep.equal({});

done();
}, fileReadTimeout);
});
});
it('should not map files with no coverage', done => {
const server = createServer(createConfig(true));
server.start();
server.on('run_complete', () => {
setTimeout(() => {
const data = JSON.parse(fs.readFileSync(OUTPUT_DETAILS_FILE));
expect(data).to.deep.equal({});

// Hacky workaround to make sure the file has been written
const output = fs.readFileSync(OUTPUT_LOG_FILE).toString();
expect(
Boolean(
output.match(
/\[DEBUG\] reporter\.coverage-istanbul - File \[\/.+test\/fixtures\/typescript\/src\/ignored-file\.ts\] ignored, nothing could be mapped/
)
)
)
).not.to.equal(false);
done();
}, fileReadTimeout);
).to.equal(true);
done();
}, fileReadTimeout);
});
});
});

Expand Down

0 comments on commit e5a5545

Please sign in to comment.