Skip to content

Commit dccb126

Browse files
rndmerlebahmutov
authored andcommitted
feat: support .vue files coverage (#17)
* feat: support .vue files coverage * fix: check there is a sourcemap before fixing pathes * Unit test the sourcemap path fixing
1 parent 7d220c3 commit dccb126

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

cypress/integration/spec.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
// https://on.cypress.io/intelligent-code-completion
33
/// <reference types="Cypress" />
44

5-
import {add} from '../unit'
5+
import { add } from '../unit'
6+
const { fixSourcePathes } = require('../../utils')
67

78
context('Page test', () => {
89
beforeEach(() => {
@@ -28,4 +29,33 @@ context('Unit tests', () => {
2829
it('concatenates strings', () => {
2930
expect(add('foo', 'Bar')).to.equal('fooBar')
3031
})
32+
33+
it('fixes webpack loader source-map path', () => {
34+
const coverage = {
35+
'/folder/module.js': {
36+
inputSourceMap: {
37+
sources: ['/folder/module.js']
38+
}
39+
},
40+
'/folder/component.vue': {
41+
inputSourceMap: {
42+
sources: [
43+
'/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&'
44+
]
45+
}
46+
},
47+
'/folder/module-without-sourcemap.js': {
48+
path: '/folder/module-without-sourcemap.js'
49+
}
50+
}
51+
52+
fixSourcePathes(coverage)
53+
54+
expect(coverage['/folder/module.js'].inputSourceMap.sources)
55+
.to.deep.equal(['/folder/module.js'])
56+
expect(coverage['/folder/component.vue'].inputSourceMap.sources)
57+
.to.deep.equal(['/folder/component.vue'])
58+
expect(coverage['/folder/module-without-sourcemap.js'].path)
59+
.to.eq('/folder/module-without-sourcemap.js')
60+
})
3161
})

task.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const { join } = require('path')
33
const { existsSync, mkdirSync, readFileSync, writeFileSync } = require('fs')
44
const execa = require('execa')
55
const debug = require('debug')('code-coverage')
6+
const { fixSourcePathes } = require('./utils')
67

78
// these are standard folder and file names used by NYC tools
89
const outputFolder = '.nyc_output'
@@ -49,6 +50,7 @@ module.exports = {
4950
* with previously collected coverage.
5051
*/
5152
combineCoverage (coverage) {
53+
fixSourcePathes(coverage)
5254
const previous = existsSync(nycFilename)
5355
? JSON.parse(readFileSync(nycFilename))
5456
: istanbul.createCoverageMap({})

utils.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = {
2+
/**
3+
* Remove potential Webpack loaders string and query parameters from sourcemap path
4+
*/
5+
fixSourcePathes (coverage) {
6+
Object.keys(coverage).forEach(file => {
7+
const sourcemap = coverage[file].inputSourceMap
8+
if (!sourcemap) return
9+
sourcemap.sources = sourcemap.sources.map(source => {
10+
let cleaned = source
11+
if (cleaned.includes('!')) cleaned = cleaned.split('!').pop()
12+
if (cleaned.includes('?')) cleaned = cleaned.split('?').shift()
13+
return cleaned
14+
})
15+
})
16+
}
17+
}

0 commit comments

Comments
 (0)