forked from fedoseev-vitaliy/mocha-rp-reporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmocha-rp-reporter.js
153 lines (129 loc) · 4.4 KB
/
mocha-rp-reporter.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"use strict";
const mocha = require('mocha');
const path = require('path');
function RPReporter(runner, options) {
mocha.reporters.Base.call(this, runner);
let config;
let launchId = null;
let suiteIds = {};
let testIds = {};
let suiteStack = [];
// load config
try {
config = options.reporterOptions.configOptions ? options.reporterOptions.configOptions : require(path.join(process.cwd(), options.reporterOptions.configFile));
} catch (err) {
console.error(`Failed to load config. Error: ${err}`);
}
let connector = new (require("./rp_connector_sync"))(config);
runner.on('pass', function(test){
});
runner.on('fail', function(test, err){
try {
connector.sendLog(testIds[test.title], {
level: connector.RP_LEVEL.FAILED,
message: err.message
});
} catch (err) {
console.error(`Failed to send log for item. Error: ${err}`);
}
});
runner.on('start', function() {
try {
let res = connector.startLaunch();
launchId = res.body.id;
} catch (err) {
console.error(`Failed to launch run. Error: ${err}`);
}
});
runner.on('end', function(){
try {
connector.finishLaunch(launchId);
} catch (err) {
console.error(`Failed to finish run. Error: ${err}`);
}
});
runner.on('suite', function(suite){
if(suite.title === "") {
return true;
} else {
try {
let res = null;
if (suiteStack.length == 0) {
res = connector.startRootItem({
name: suite.title,
launch: launchId,
description: suite.fullTitle(),
type: connector.RP_ITEM_TYPE.SUITE
});
} else {
res = connector.startChildItem({
name: suite.title,
launch: launchId,
description: suite.fullTitle(),
type: connector.RP_ITEM_TYPE.SUITE
}, suiteIds[suiteStack[suiteStack.length - 1].title]);
}
suiteStack.push(suite);
if (res)
suiteIds[suite.title] = res.body.id;
} catch (err) {
console.error(`Failed to create root item. Error: ${err}`);
}
}
});
runner.on('suite end', function(suite){
try {
connector.finishItem({
status: suite.tests.filter(test => test.state === "failed").length > 0 ? "failed" : "passed",
id: suiteIds[suite.title]
});
suiteStack.pop();
} catch (err) {
console.error(`Failed to create child item. Error: ${err}`);
}
});
runner.on('test', function(test){
try {
let res = connector.startChildItem({
name: test.title,
launch: launchId,
description: test.fullTitle(),
type: connector.RP_ITEM_TYPE.TEST
}, suiteIds[test.parent.title]);
testIds[test.title] = res.body.id;
} catch (err) {
console.error(`Failed to create child item. Error: ${err}`);
}
});
runner.on('pending', function (test) {
try {
let res = connector.startChildItem({
name: test.title,
launch: launchId,
description: test.fullTitle(),
type: connector.RP_ITEM_TYPE.TEST
}, suiteIds[test.parent.title]);
connector.sendLog(res.body.id, {
level: connector.RP_LEVEL.SKIPPED,
message: test.title
})
connector.finishItem({
status: connector.RP_STATUS.SKIPPED,
id: res.body.id
});
} catch (err) {
console.error(`Failed to create child item. Error: ${err}`);
}
});
runner.on('test end', function(test){
try {
connector.finishItem({
status: test.state,
id: testIds[test.title]
});
} catch (err) {
console.error(`Failed to create child item. Error: ${err}`);
}
});
}
module.exports = RPReporter;