Skip to content

Commit

Permalink
fix: prepend the webpack context to the source root if not set
Browse files Browse the repository at this point in the history
Closes #32
  • Loading branch information
Matt Lewis committed Dec 23, 2017
1 parent 02a46e1 commit 4138b80
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function CoverageIstanbulReporter(baseReporterDecorator, logger, config) {
Object.keys(coverage).forEach(filename => {
const fileCoverage = coverage[filename];
if (fileCoverage.inputSourceMap && coverageIstanbulReporter.fixWebpackSourcePaths) {
fileCoverage.inputSourceMap = util.fixWebpackSourcePaths(fileCoverage.inputSourceMap);
fileCoverage.inputSourceMap = util.fixWebpackSourcePaths(fileCoverage.inputSourceMap, config.webpack);
}
if (
coverageIstanbulReporter.skipFilesWithNoCoverage &&
Expand Down
10 changes: 9 additions & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,16 @@ function fixWebpackFilePath(filePath) {
return filePath;
}

function fixWebpackSourcePaths(sourceMap) {
function fixWebpackSourcePaths(sourceMap, webpackConfig) {
let sourceRoot = sourceMap.sourceRoot;
// Fix for https://github.com/mattlewis92/karma-coverage-istanbul-reporter/issues/32
// The sourceRoot is relative to the project directory and not an absolute path, so add the webpack context to it if set
if (webpackConfig && webpackConfig.context && sourceMap.sourceRoot && !sourceMap.sourceRoot.startsWith(webpackConfig.context)) {
sourceRoot = path.join(webpackConfig.context, sourceRoot);
}

return Object.assign({}, sourceMap, {
sourceRoot: sourceRoot, // eslint-disable-line object-shorthand
sources: (sourceMap.sources || []).map(source => {
source = fixWebpackFilePath(source);
if (sourceMap.sourceRoot && source.startsWith(sourceMap.sourceRoot)) {
Expand Down
34 changes: 34 additions & 0 deletions test/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,39 @@ describe('util', () => {

expect(fixWebpackSourcePaths(input)).to.deep.equal(output);
});

it('should add the webpack context to the source root if not set', () => {
const input = {
file: 'example.ts',
sourceRoot: 'src',
sources: ['example.ts']
};

const output = {
file: 'example.ts',
sourceRoot: '/Users/mattlewis/Code/open-source/karma-coverage-istanbul-reporter/test/fixtures/typescript/src',
sources: ['example.ts']
};
expect(fixWebpackSourcePaths(input, {
context: '/Users/mattlewis/Code/open-source/karma-coverage-istanbul-reporter/test/fixtures/typescript'
})).to.deep.equal(output);
});

it('should only add the webpack context to the source root if not already set', () => {
const input = {
file: 'example.ts',
sourceRoot: '/Users/mattlewis/Code/open-source/karma-coverage-istanbul-reporter/test/fixtures/typescript/src',
sources: ['example.ts']
};

const output = {
file: 'example.ts',
sourceRoot: '/Users/mattlewis/Code/open-source/karma-coverage-istanbul-reporter/test/fixtures/typescript/src',
sources: ['example.ts']
};
expect(fixWebpackSourcePaths(input, {
context: '/Users/mattlewis/Code/open-source/karma-coverage-istanbul-reporter/test/fixtures/typescript'
})).to.deep.equal(output);
});
});
});

0 comments on commit 4138b80

Please sign in to comment.