From d60a0759879ef8dad3e67345e7636acb3f71946c Mon Sep 17 00:00:00 2001 From: Renaud Merle Date: Thu, 20 Jun 2019 00:24:08 +0200 Subject: [PATCH 1/3] feat: support .vue files coverage --- task.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/task.js b/task.js index 5b4264e0..6082b987 100644 --- a/task.js +++ b/task.js @@ -18,6 +18,19 @@ function saveCoverage (coverage) { writeFileSync(nycFilename, JSON.stringify(coverage, null, 2)) } +// Remove potential Webpack loaders string and query parameters from sourcemap path +function fixSourcePathes (coverage) { + Object.keys(coverage).forEach(file => { + const sourcemap = coverage[file].inputSourceMap + 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 + }) + }) +} + module.exports = { /** * Clears accumulated code coverage information. @@ -49,6 +62,7 @@ module.exports = { * with previously collected coverage. */ combineCoverage (coverage) { + fixSourcePathes(coverage) const previous = existsSync(nycFilename) ? JSON.parse(readFileSync(nycFilename)) : istanbul.createCoverageMap({}) From 4fd6b90b40ca961be064e38a7b7aaa883c7e7108 Mon Sep 17 00:00:00 2001 From: Renaud Merle Date: Thu, 20 Jun 2019 00:49:42 +0200 Subject: [PATCH 2/3] fix: check there is a sourcemap before fixing pathes --- task.js | 1 + 1 file changed, 1 insertion(+) diff --git a/task.js b/task.js index 6082b987..e6a42cb5 100644 --- a/task.js +++ b/task.js @@ -22,6 +22,7 @@ function saveCoverage (coverage) { function 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() From 1e4d24508dac34e851cdd8a1c804d215ba3c1f16 Mon Sep 17 00:00:00 2001 From: Renaud Merle Date: Fri, 21 Jun 2019 00:26:14 +0200 Subject: [PATCH 3/3] Unit test the sourcemap path fixing --- cypress/integration/spec.js | 32 +++++++++++++++++++++++++++++++- task.js | 15 +-------------- utils.js | 17 +++++++++++++++++ 3 files changed, 49 insertions(+), 15 deletions(-) create mode 100644 utils.js 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 e6a42cb5..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' @@ -18,20 +19,6 @@ function saveCoverage (coverage) { writeFileSync(nycFilename, JSON.stringify(coverage, null, 2)) } -// Remove potential Webpack loaders string and query parameters from sourcemap path -function 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 - }) - }) -} - module.exports = { /** * Clears accumulated code coverage information. 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