-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
346 lines (302 loc) · 9.81 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*!
*
* Copyright (c) 2013 Sebastian Golasch
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
'use strict';
// ext. libs
var fs = require('fs');
var path = require('path');
var jsonxml = require('jsontoxml');
// int. global
var reporter = null;
/**
* The jUnit reporter can produce a jUnit compatible file with the results of your testrun,
* this reporter enables you to use daleks testresults within a CI environment like Jenkins.
*
* The reporter can be installed with the following command:
*
* ```bash
* $ npm install dalek-reporter-junit --save-dev
* ```
*
* The file will follow the jUnit XML format:
*
* ```html
* <?xml version="1.0" encoding="utf-8"?>
* <resource name="DalekJSTest">
* <testsuite start="1375125067" name="Click - DalekJS guinea pig [Phantomjs]" end="1375125067" totalTests="1">
* <testcase start="1375125067" name="Can click a select option (OK, jQuery style, no message)" end="1375125067" result="pass">
* <variation start="1375125067" name="val" end="1375125067">
* <severity>pass</severity>
* <description><![CDATA[David is the favourite]]></description>
* <resource>DalekJSTest</resource>
* </variation>
* <variation start="1375125067" name="val" end="1375125067">
* <severity>pass</severity>
* <description><![CDATA[Matt is now my favourite, bow ties are cool]]></description>
* <resource>DalekJSTest</resource>
* </variation>
* </testcase>
* </testsuite>
* </resource>
* ```
*
* By default the file will be written to `report/dalek.xml`,
* you can change this by adding a config option to the your Dalekfile
*
* ```javascript
* "junit-reporter": {
* "dest": "your/folder/your_file.xml"
* }
* ```
*
* If you would like to use the reporter (in addition to the std. console reporter),
* you can start dalek with a special command line argument
*
* ```bash
* $ dalek your_test.js -r console,junit
* ```
*
* or you can add it to your Dalekfile
*
* ```javascript
* "reporter": ["console", "junit"]
* ```
*
* @class Reporter
* @constructor
* @part JUnit
* @api
*/
function Reporter (opts) {
this.events = opts.events;
this.config = opts.config;
this.testCount = 0;
this.testIdx = -1;
this.variationCount = -1;
this.data = {};
this.data.tests = [];
this.browser = null;
var defaultReportFolder = 'report';
this.dest = this.config.get('junit-reporter') && this.config.get('junit-reporter').dest ? this.config.get('junit-reporter').dest : defaultReportFolder;
// prepare base xml
this.xml = [
{
name: 'resource',
attrs: {
name:'DalekJSTest'
},
children: []
}
];
this.startListening();
}
/**
* @module Reporter
*/
module.exports = function (opts) {
if (reporter === null) {
reporter = new Reporter(opts);
}
return reporter;
};
Reporter.prototype = {
/**
* Connects to all the event listeners
*
* @method startListening
* @chainable
*/
startListening: function () {
this.events.on('report:run:browser', this.runBrowser.bind(this));
this.events.on('report:assertion', this.assertion.bind(this));
this.events.on('report:test:started', this.testStarted.bind(this));
this.events.on('report:test:finished', this.testFinished.bind(this));
this.events.on('report:runner:finished', this.runnerFinished.bind(this));
this.events.on('report:testsuite:started', this.testsuiteStarted.bind(this));
//this.events.on('report:testsuite:finished', this.testsuiteFinished.bind(this));
return this;
},
/**
* Stores the current browser name
*
* @method runBrowser
* @param {string} browser Browser name
* @chainable
*/
runBrowser: function (browser) {
this.browser = browser;
return this;
},
/**
* Generates XML skeleton for testsuites
*
* @method testsuiteStarted
* @param {string} name Testsuite name
* @chainable
*/
testsuiteStarted: function (name) {
this.testCount = 0;
this.testIdx++;
this.xml[0].children.push({
name: 'testsuite',
children: [],
attrs: {
name: name + ' [' + this.browser + ']',
}
});
return this;
},
/**
* Finishes XML skeleton for testsuites
*
* @method testsuiteFinished
* @chainable
*/
testsuiteFinished: function () {
// this.xml[0].children[this.testIdx].attrs.end = Math.round(new Date().getTime() / 1000);
return this;
},
/**
* Generates XML skeleton for an assertion
*
* @method assertion
* @param {object} data Event data
* @chainable
*/
assertion: function (data) {
if (! data.success) {
//var timestamp = Math.round(new Date().getTime() / 1000);
this.xml[0].children[this.testIdx].children[this.testCount].children.push({
name: 'failure',
attrs: {
name: data.type,
message: (data.message ? data.message : 'Expected: ' + data.expected + 'Actual: ' + data.value)
}
});
//if (this.variationCount > -1 && this.xml[0].children[this.testIdx].children[this.testCount].children[this.variationCount]) {
//this.xml[0].children[this.testIdx].children[this.testCount].children[this.variationCount].attrs.end = timestamp;
//}
this.variationCount++;
}
return this;
},
/**
* Generates XML skeleton for a testcase
*
* @method testStarted
* @param {object} data Event data
* @chainable
*/
testStarted: function (data) {
this.variationCount = -1;
this.xml[0].children[this.testIdx].children.push({
name: 'testcase',
children: [],
attrs: {
classname: this.xml[0].children[this.testIdx].attrs.name,
name: data.name
}
});
return this;
},
/**
* Finishes XML skeleton for a testcase
*
* @method testFinished
* @param {object} data Event data
* @chainable
*/
testFinished: function () {
//var timestamp = Math.round(new Date().getTime() / 1000);
if (this._checkNodeAttributes(this.testIdx, this.testCount)) {
this.xml[0].children[this.testIdx].children[this.testCount].attrs = {};
}
//this.xml[0].children[this.testIdx].children[this.testCount].attrs.end = timestamp;
//this.xml[0].children[this.testIdx].children[this.testCount].attrs.result = data.status ? 'Passed' : 'Failed';
if (this.variationCount > -1) {
if (this._checkNodeAttributes(this.testIdx, this.testCount, this.variationCount)) {
this.xml[0].children[this.testIdx].children[this.testCount].children[this.variationCount].attrs = {};
}
//this.xml[0].children[this.testIdx].children[this.testCount].children[this.variationCount].attrs.end = timestamp;
}
this.testCount++;
this.variationCount = -1;
return this;
},
/**
* Finishes XML and writes file to the file system
*
* @method runnerFinished
* @param {object} data Event data
* @chainable
*/
runnerFinished: function (data) {
this.data.elapsedTime = data.elapsedTime;
this.data.status = data.status;
this.data.assertions = data.assertions;
this.data.assertionsFailed = data.assertionsFailed;
this.data.assertionsPassed = data.assertionsPassed;
var contents = jsonxml(this.xml, {escape: true, removeIllegalNameCharacters: true, prettyPrint: true, xmlHeader: 'version="1.0" encoding="UTF-8"'});
if (path.extname(this.dest) !== '.xml') {
this.dest = this.dest + '/dalek.xml';
}
this.events.emit('report:written', {type: 'junit', dest: this.dest});
this._recursiveMakeDirSync(path.dirname(this.dest.replace(path.basename(this.dest, ''))));
fs.writeFileSync(this.dest, contents, 'utf8');
},
/**
* Helper method to generate deeper nested directory structures
*
* @method _recursiveMakeDirSync
* @param {string} path PAth to create
*/
_recursiveMakeDirSync: function (path) {
var pathSep = require('path').sep;
var dirs = path.split(pathSep);
var root = '';
while (dirs.length > 0) {
var dir = dirs.shift();
if (dir === '') {
root = pathSep;
}
if (!fs.existsSync(root + dir)) {
fs.mkdirSync(root + dir);
}
root += dir + pathSep;
}
},
/**
* Helper method to check if attributes should be set to an empty object literal
*
* @method _checkNodeAttributes
* @param {string} testIdx Id of the test node
* @param {string} testCount Id of the child node
* @param {string} variationCount Id of the testCount child node
*/
_checkNodeAttributes: function (testIdx, testCount, variationCount) {
if (variationCount === undefined) {
return typeof this.xml[0].children[testIdx].children[testCount].attrs === 'undefined';
}
return typeof this.xml[0].children[testIdx].children[testCount].children[variationCount].attrs === 'undefined';
}
};