-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
genTestsSummary.js
109 lines (92 loc) · 2.58 KB
/
genTestsSummary.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import {
SEVERITY_COUNT_WARN,
SEVERITY_COUNT_ERROR,
SEVERITY_GROUP_WARN,
SEVERITY_GROUP_ERROR,
TEST_COUNT,
} from 'resultKeys';
import { useSuiteId, useTestObjects, useSkippedTests } from 'stateHooks';
/**
* Reads the testObjects list and gets full validation result from it.
*/
const genTestsSummary = () => {
const [testObjects] = useTestObjects();
const [suiteIdState] = useSuiteId();
const [skippedTests] = useSkippedTests();
const summary = {
[SEVERITY_COUNT_ERROR]: 0,
[SEVERITY_COUNT_WARN]: 0,
[TEST_COUNT]: 0,
groups: {},
name: suiteIdState.name,
tests: {},
};
appendSummary(testObjects);
appendSummary(skippedTests, true);
return countFailures(summary);
function appendSummary(testObject, skipped) {
testObject.forEach(testObject => {
const { fieldName, groupName } = testObject;
summary.tests[fieldName] = genTestObject(
summary.tests,
testObject,
skipped
);
if (groupName) {
summary.groups[groupName] = summary.groups[groupName] || {};
summary.groups[groupName][fieldName] = genTestObject(
summary.groups[groupName],
testObject,
skipped
);
}
});
}
};
/**
* Counts the failed tests and adds global counters
* @param {Object} summary (generated by genTestsSummary)
*/
export const countFailures = summary => {
for (const test in summary.tests) {
summary[SEVERITY_COUNT_ERROR] += summary.tests[test][SEVERITY_COUNT_ERROR];
summary[SEVERITY_COUNT_WARN] += summary.tests[test][SEVERITY_COUNT_WARN];
summary[TEST_COUNT] += summary.tests[test][TEST_COUNT];
}
return summary;
};
export default genTestsSummary;
/**
*
* @param {Object} summaryKey The container for the test result data
* @param {VestTest} testObject
* @returns {Object} Test result summary
*/
const genTestObject = (summaryKey, testObject, skipped) => {
const { fieldName, isWarning, failed, statement } = testObject;
summaryKey[fieldName] = summaryKey[fieldName] || {
[SEVERITY_COUNT_ERROR]: 0,
[SEVERITY_COUNT_WARN]: 0,
[TEST_COUNT]: 0,
};
const testKey = summaryKey[fieldName];
if (skipped) {
return testKey;
}
summaryKey[fieldName][TEST_COUNT]++;
// Adds to severity group
const addTo = (count, group) => {
testKey[count]++;
if (statement) {
testKey[group] = (testKey[group] || []).concat(statement);
}
};
if (failed) {
if (isWarning) {
addTo(SEVERITY_COUNT_WARN, SEVERITY_GROUP_WARN);
} else {
addTo(SEVERITY_COUNT_ERROR, SEVERITY_GROUP_ERROR);
}
}
return testKey;
};