forked from cypress-io/code-coverage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupport-utils.js
67 lines (56 loc) · 2.13 KB
/
support-utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// @ts-check
// helper functions that are safe to use in the browser
// from support.js file - no file system access
/**
* remove coverage for the spec files themselves,
* only keep "external" application source file coverage
*/
const filterSpecsFromCoverage = (totalCoverage, config = Cypress.config) => {
const integrationFolder = config('integrationFolder')
// @ts-ignore
const testFilePattern = config('testFiles')
// test files chould be:
// wild card string "**/*.*" (default)
// wild card string "**/*spec.js"
// list of wild card strings or names ["**/*spec.js", "spec-one.js"]
const testFilePatterns = Array.isArray(testFilePattern)
? testFilePattern
: [testFilePattern]
const isUsingDefaultTestPattern = testFilePattern === '**/*.*'
const isTestFile = filename => {
const matchedPattern = testFilePatterns.some(specPattern =>
Cypress.minimatch(filename, specPattern)
)
const matchedEndOfPath = testFilePatterns.some(specPattern =>
filename.endsWith(specPattern)
)
return matchedPattern || matchedEndOfPath
}
const isInIntegrationFolder = filename =>
filename.startsWith(integrationFolder)
const isA = (fileCoverge, filename) => isInIntegrationFolder(filename)
const isB = (fileCoverge, filename) => isTestFile(filename)
const isTestFileFilter = isUsingDefaultTestPattern ? isA : isB
const coverage = Cypress._.omitBy(totalCoverage, isTestFileFilter)
return coverage
}
/**
* Replace source-map's path by the corresponding absolute file path
* (coverage report wouldn't work with source-map path being relative
* or containing Webpack loaders and query parameters)
*/
function fixSourcePaths(coverage) {
Object.values(coverage).forEach(file => {
const { path: absolutePath, inputSourceMap } = file
const fileName = /([^\/\\]+)$/.exec(absolutePath)[1]
if (!inputSourceMap || !fileName) return
if (inputSourceMap.sourceRoot) inputSourceMap.sourceRoot = ''
inputSourceMap.sources = inputSourceMap.sources.map(source =>
source.includes(fileName) ? absolutePath : source
)
})
}
module.exports = {
fixSourcePaths,
filterSpecsFromCoverage
}