diff --git a/cypress/integration/spec.js b/cypress/integration/spec.js index b0eef638..e239c4fd 100644 --- a/cypress/integration/spec.js +++ b/cypress/integration/spec.js @@ -2,7 +2,8 @@ // https://on.cypress.io/intelligent-code-completion /// -import {add} from '../unit' +import { add } from '../unit' +const { fixSourcePathes } = require('../../utils') context('Page test', () => { beforeEach(() => { @@ -28,4 +29,33 @@ context('Unit tests', () => { it('concatenates strings', () => { expect(add('foo', 'Bar')).to.equal('fooBar') }) + + it('fixes webpack loader source-map path', () => { + const coverage = { + '/folder/module.js': { + inputSourceMap: { + sources: ['/folder/module.js'] + } + }, + '/folder/component.vue': { + inputSourceMap: { + sources: [ + '/folder/node_modules/cache-loader/dist/cjs.js??ref--0-0!/folder/node_modules/vue-loader/lib/index.js??vue-loader-options!/folder/component.vue?vue&type=script&lang=ts&' + ] + } + }, + '/folder/module-without-sourcemap.js': { + path: '/folder/module-without-sourcemap.js' + } + } + + fixSourcePathes(coverage) + + expect(coverage['/folder/module.js'].inputSourceMap.sources) + .to.deep.equal(['/folder/module.js']) + expect(coverage['/folder/component.vue'].inputSourceMap.sources) + .to.deep.equal(['/folder/component.vue']) + expect(coverage['/folder/module-without-sourcemap.js'].path) + .to.eq('/folder/module-without-sourcemap.js') + }) }) diff --git a/task.js b/task.js index 5b4264e0..0af262d6 100644 --- a/task.js +++ b/task.js @@ -3,6 +3,7 @@ const { join } = require('path') const { existsSync, mkdirSync, readFileSync, writeFileSync } = require('fs') const execa = require('execa') const debug = require('debug')('code-coverage') +const { fixSourcePathes } = require('./utils') // these are standard folder and file names used by NYC tools const outputFolder = '.nyc_output' @@ -49,6 +50,7 @@ module.exports = { * with previously collected coverage. */ combineCoverage (coverage) { + fixSourcePathes(coverage) const previous = existsSync(nycFilename) ? JSON.parse(readFileSync(nycFilename)) : istanbul.createCoverageMap({}) diff --git a/utils.js b/utils.js new file mode 100644 index 00000000..882c6245 --- /dev/null +++ b/utils.js @@ -0,0 +1,17 @@ +module.exports = { + /** + * Remove potential Webpack loaders string and query parameters from sourcemap path + */ + fixSourcePathes (coverage) { + Object.keys(coverage).forEach(file => { + const sourcemap = coverage[file].inputSourceMap + if (!sourcemap) return + sourcemap.sources = sourcemap.sources.map(source => { + let cleaned = source + if (cleaned.includes('!')) cleaned = cleaned.split('!').pop() + if (cleaned.includes('?')) cleaned = cleaned.split('?').shift() + return cleaned + }) + }) + } +} \ No newline at end of file