forked from hermannpencole/mocha-html-reporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
196 lines (177 loc) · 5.23 KB
/
index.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
var util = require('util'),
events = require('events'),
fs = require('fs'),
HTML = require('html-generate'),
writeson = require("writeson");
var path = __dirname;
function camelize (str) {
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function(letter, index) {
return index == 0 ? letter.toLowerCase() : letter.toUpperCase();
}).replace(/\s+/g, '');
}
function makeTestData (suite) {
var id = camelize(suite.parent + suite.title),
icon,
time;
if (suite.event == 'test:pass') {
icon = '<span style="color:#BFEB50">✔</span>';
time = suite.duration + 'ms';
} if (suite.event == 'test:fail') {
icon = '<span style="color:#F51800">✘</span>';
time = suite.duration + 'ms';
} if (suite.event == 'test:pending') {
icon = '<span style="color:#42A5F5">~</span>';
time = 'n/a';
}
return {
tagName: 'tr',
children: [
{
tagName: 'td',
text: suite.title
},
{
tagName: 'td',
html: icon
},
{
tagName: 'td',
html: '<button data-target="' + id + '" class="btn modal-trigger">Source</button>'
},
{
tagName: 'td',
text: time
}
]
}
}
function makeSourceData (suite) {
var id = camelize(suite.parent + suite.title);
return {
tagName: 'div',
attributes: {
id: id,
class: 'modal'
},
children: [
{
tagName: 'div',
attributes: {
class: 'modal-content'
},
children: [
{
tagName: 'h3',
text: suite.parent
},
{
tagName: 'h4',
text: suite.title
},
{
tagName: 'p',
text: JSON.stringify(suite)
}
]
},
{
tagName: 'div',
attributes: {
class: 'modal-footer'
},
children: [
{
tagName: 'a',
attributes: {
href: '#',
class: 'modal-action modal-close waves-effect waves-green btn-flat'
},
text: 'Close'
}
]
}
]
}
}
var CustomReporter = function() {
var result = {};
var testTable = {};
var modal = {};
this.on('suite:start', function(suite) {
result[suite.title] = {};
testTable[suite.title] = [];
modal[suite.title] = [];
});
this.on('test:pass', function(suite) {
if (!result[suite.parent].pass) {
result[suite.parent].pass = 0;
}
result[suite.parent].pass++;
for(var key in testTable) {
if (key == suite.parent) {
testTable[key].push(makeTestData(suite));
modal[key].push(makeSourceData(suite));
}
}
});
this.on('test:fail', function(suite) {
if (!result[suite.parent].fail) {
result[suite.parent].fail = 0;
}
result[suite.parent].fail++;
for(var key in testTable) {
if (key == suite.parent) {
testTable[key].push(makeTestData(suite));
modal[key].push(makeSourceData(suite));
}
}
});
this.on('test:pending', function(suite) {
var icon = '<span style="color:#42A5F5">~</span>',
id = camelize(suite.parent + suite.title);
if (!result[suite.parent].pending) {
result[suite.parent].pending = 0;
}
result[suite.parent].pending++;
for(var key in testTable) {
if (key == suite.parent) {
testTable[key].push(makeTestData(suite));
modal[key].push(makeSourceData(suite));
}
}
});
this.on('suite:end', function(value) {
// ..do something
});
var final = [];
this.on('end', function() {
for(var key in result) {
final.push({
"results": key,
"name": "Pass",
"# of tests": result[key].pass
});
final.push({
"results": key,
"name": "Fail",
"# of tests": result[key].fail
});
final.push({
"results": key,
"name": "Pending",
"# of tests": result[key].pending
});
}
writeson.sync(path + '/reports/data/data.json', final, "\t");
writeson.sync(path + '/reports/data/tests.json', testTable, "\t");
writeson.sync(path + '/reports/data/source.json', modal, "\t");
});
};
/**
* Inherit from EventEmitter
*/
util.inherits(CustomReporter, events.EventEmitter);
/**
* Expose Custom Reporter
*/
module.exports = exports = CustomReporter;