-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon-utils.js
87 lines (76 loc) · 2 KB
/
common-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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// @ts-check
function stringToArray(prop, obj) {
if (typeof obj[prop] === 'string') {
obj[prop] = [obj[prop]]
}
return obj
}
function combineNycOptions(...options) {
// last option wins
const nycOptions = Object.assign({}, ...options)
// normalize string and [string] props
stringToArray('reporter', nycOptions)
stringToArray('extension', nycOptions)
stringToArray('exclude', nycOptions)
return nycOptions
}
const defaultNycOptions = {
'report-dir': './coverage',
reporter: ['lcov', 'clover', 'json', 'json-summary'],
extension: ['.js', '.cjs', '.mjs', '.ts', '.tsx', '.jsx'],
excludeAfterRemap: false,
}
/**
* Returns an object with placeholder properties for files we
* do not have coverage yet. The result can go into the coverage object
*
* @param {string} fullPath Filename
*/
const fileCoveragePlaceholder = (fullPath) => {
return {
path: fullPath,
statementMap: {},
fnMap: {},
branchMap: {},
s: {},
f: {},
b: {},
}
}
const isPlaceholder = (entry) => {
// when the file has been instrumented, its entry has "hash" property
return !('hash' in entry)
}
/**
* Given a coverage object with potential placeholder entries
* inserted instead of covered files, removes them. Modifies the object in place
*/
const removePlaceholders = (coverage) => {
Object.keys(coverage).forEach((key) => {
if (isPlaceholder(coverage[key])) {
delete coverage[key]
}
})
}
/**
* Returns true if the user disabled the plugin using the env object.
*/
function isPluginDisabled(cyEnv) {
if (cyEnv.coverage === false) {
return true
}
if (typeof cyEnv.coverage === 'object') {
// the user explicitly disabled the plugin
// be kind and accept both "disable" and "disabled" options
return cyEnv.coverage.disable === true || cyEnv.coverage.disabled === true
}
// by default the plugin is enabled
return false
}
module.exports = {
combineNycOptions,
defaultNycOptions,
fileCoveragePlaceholder,
removePlaceholders,
isPluginDisabled,
}