-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
94 lines (83 loc) · 2.32 KB
/
index.ts
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
import WDIOReporter from "@wdio/reporter";
import fs from "fs";
const REGEX_SUITE_AND_TEST_ID = /\bS(\d+)C(\d+)\b/g;
export default class QualityWatcherReporter extends WDIOReporter {
private _stateCounts = { passed: 0, failed: 0, skipped: 0, total: 0 };
private results = new Map();
private dir = "./QualityWatcher";
constructor(options) {
super(options);
options = Object.assign(options, { stdout: true });
}
onTestPass() {
this._stateCounts.passed++;
}
onTestFail() {
this._stateCounts.failed++;
}
onTestSkip() {
this._stateCounts.skipped++;
}
onTestEnd(test) {
try {
const { suite_id, test_id } = this.getSuiteAndCaseIds(test.title);
const testCaseDetails = {
comment: test.error?.message ? test.error?.message?.replace(/\x1b\[[0-9;]*m/g, '') : test.title,
status: test.state,
time: test.duration || 0,
attachments: [],
suite_id: suite_id || undefined,
test_id: test_id || undefined,
};
if (testCaseDetails.suite_id && testCaseDetails.test_id) {
const testKey = `${testCaseDetails.suite_id}-${testCaseDetails.test_id}`;
this.results.set(testKey, testCaseDetails);
} else {
const newCaseData = {
case: {
suiteTitle: test.parent,
testCaseTitle: test.title,
steps: ""
},
...testCaseDetails
}
const testKey = `${test.parent}-${test.title}`;
this.results.set(testKey, newCaseData);
}
} catch (error) {
console.error(error);
}
}
onSuiteEnd(suite) {
try {
this.checkDirectory(this.dir);
fs.writeFileSync(
this.dir + `/${suite.title}.json`,
JSON.stringify(Array.from(this.results.values()), null, 2)
);
} catch (err) {
console.error(err);
}
}
getSuiteAndCaseIds(title) {
let suiteAndCaseIds;
let suiteId;
let caseId;
while ((suiteAndCaseIds = REGEX_SUITE_AND_TEST_ID.exec(title)) != null) {
suiteId = suiteAndCaseIds[1];
caseId = suiteAndCaseIds[2];
}
return {
suite_id: Number(suiteId),
test_id: Number(caseId),
};
}
checkDirectory(dir) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
}
generateRandomString() {
return (Math.random() + 1).toString(36).substring(7);
}
}