-
Notifications
You must be signed in to change notification settings - Fork 108
/
common-utils.js
69 lines (60 loc) · 1.53 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
// @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]
}
})
}
module.exports = {
combineNycOptions,
defaultNycOptions,
fileCoveragePlaceholder,
removePlaceholders
}