diff --git a/src/util.js b/src/util.js index 7564eee..be93e41 100644 --- a/src/util.js +++ b/src/util.js @@ -16,7 +16,7 @@ function fixWebpackFilePath(filePath) { function fixPathSeparators(filePath) { const isWin = process.platform.startsWith('win'); // Workaround for https://github.com/mattlewis92/karma-coverage-istanbul-reporter/issues/9 - if (isWin) { + if (isWin && filePath) { return filePath.replace(/\//g, '\\'); } return filePath; @@ -38,9 +38,8 @@ function fixWebpackSourcePaths(sourceMap, webpackConfig) { sourceRoot = fixPathSeparators(sourceRoot); - return Object.assign({}, sourceMap, { + const result = Object.assign({}, sourceMap, { file: fixPathSeparators(sourceMap.file), - sourceRoot, sources: (sourceMap.sources || []).map(source => { source = fixWebpackFilePath(source); if (sourceRoot && source.startsWith(sourceRoot)) { @@ -49,6 +48,12 @@ function fixWebpackSourcePaths(sourceMap, webpackConfig) { return source; }) }); + + if (sourceRoot) { + result.sourceRoot = sourceRoot; + } + + return result; } function isAbsolute(file) { diff --git a/test/util.spec.js b/test/util.spec.js index 32af7bb..2c36b9a 100644 --- a/test/util.spec.js +++ b/test/util.spec.js @@ -78,6 +78,29 @@ describe('util', () => { expect(fixWebpackSourcePaths(input)).to.deep.equal(output); }); + it('should handle undefined source roots', () => { + Object.defineProperty(process, 'platform', { + value: 'win32' + }); + const input = { + file: + 'C:/development/git/coverage-istanbul-reporter-path/client/modules/app/app.component.ts', + sources: [ + 'C:\\development\\git\\coverage-istanbul-reporter-path\\client\\modules\\app\\app.component.ts' + ] // eslint-disable-line unicorn/escape-case + }; + + const output = { + file: + 'C:\\development\\git\\coverage-istanbul-reporter-path\\client\\modules\\app\\app.component.ts', + sources: [ + 'C:\\development\\git\\coverage-istanbul-reporter-path\\client\\modules\\app\\app.component.ts' + ] + }; + + expect(fixWebpackSourcePaths(input)).to.deep.equal(output); + }); + it('should not correct path separators on non windows systems', () => { const input = { file: '/foo/bar',