From 4138b80c5eb571e9b080868e7c9d8f7f49d11ec5 Mon Sep 17 00:00:00 2001 From: Matt Lewis Date: Sat, 23 Dec 2017 18:13:10 +0000 Subject: [PATCH] fix: prepend the webpack context to the source root if not set Closes #32 --- src/reporter.js | 2 +- src/util.js | 10 +++++++++- test/util.spec.js | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/reporter.js b/src/reporter.js index e817567..6103399 100644 --- a/src/reporter.js +++ b/src/reporter.js @@ -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 && diff --git a/src/util.js b/src/util.js index 2198094..ef5761e 100644 --- a/src/util.js +++ b/src/util.js @@ -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)) { diff --git a/test/util.spec.js b/test/util.spec.js index b109b7d..336b312 100644 --- a/test/util.spec.js +++ b/test/util.spec.js @@ -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); + }); }); });