/
xunit.spec.js
439 lines (393 loc) · 12.6 KB
/
xunit.spec.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
'use strict';
var fs = require('fs');
var mkdirp = require('mkdirp');
var path = require('path');
var assert = require('assert');
var createStatsCollector = require('../../lib/stats-collector');
var EventEmitter = require('events').EventEmitter;
var reporters = require('../../').reporters;
var XUnit = reporters.XUnit;
describe('XUnit reporter', function() {
var stdout;
var stdoutWrite;
// the runner parameter of the reporter
var runner;
var callbackArgument = null;
var expectedFailure = 'some-failures';
var expectedLine = 'some-line';
var expectedClassName = 'fullTitle';
var expectedTitle = 'some title';
var expectedMessage = 'some message';
var expectedDiff =
'\n + expected - actual\n\n -foo\n +bar\n ';
var expectedStack = 'some-stack';
var expectedWrite = null;
beforeEach(function() {
stdout = [];
runner = {on: function() {}, once: function() {}};
createStatsCollector(runner);
});
describe('if reporter options output is given', function() {
var expectedOutput = 'some-output';
var options = {
reporterOptions: {
output: expectedOutput
}
};
describe('but it cant create a write stream', function() {
it('should throw expected error', function() {
var fsCreateWriteStream = fs.createWriteStream;
fs.createWriteStream = false;
var boundXUnit = XUnit.bind({}, runner, options);
expect(boundXUnit, 'to throw', 'file output not supported in browser');
fs.createWriteStream = fsCreateWriteStream;
});
});
describe('and it can create a write stream', function() {
it('should locate the output dir, create it, then assign as fileStream', function() {
var expectedDirectory;
var mkdirpSync = mkdirp.sync;
var pathDirname = path.dirname;
var fsCreateWriteStream = fs.createWriteStream;
mkdirp.sync = function(directory) {
expectedDirectory = directory;
};
path.dirname = function(location) {
return location;
};
fs.createWriteStream = function(streamDetails) {
return streamDetails;
};
var contextVariables = {
fileStream: null
};
XUnit.call(contextVariables, runner, options);
expect(expectedDirectory, 'to be', expectedOutput);
expect(contextVariables.fileStream, 'to be', expectedOutput);
fs.createWriteStream = fsCreateWriteStream;
mkdirp.sync = mkdirpSync;
path.dirname = pathDirname;
});
});
});
describe("on 'pending', 'pass' and 'fail' events", function() {
it("should add test to tests called on 'end' event", function() {
var pendingTest = {
name: 'pending',
slow: function() {}
};
var failTest = {
name: 'fail',
slow: function() {}
};
var passTest = {
name: 'pass',
slow: function() {}
};
runner.on = runner.once = function(event, callback) {
if (event === 'pending') {
callback(pendingTest);
}
if (event === 'pass') {
callback(passTest);
}
if (event === 'fail') {
callback(failTest);
}
if (event === 'end') {
callback();
}
};
var calledTests = [];
XUnit.call(
{
write: function() {},
test: function(test) {
calledTests.push(test);
}
},
runner
);
var expectedCalledTests = [pendingTest, passTest, failTest];
expect(calledTests, 'to equal', expectedCalledTests);
});
});
describe('done', function() {
describe('if fileStream is truthly', function() {
it('should run callback with failure inside streams end', function() {
var xunit = new XUnit({on: function() {}, once: function() {}});
var callback = function(failures) {
callbackArgument = failures;
};
var calledEnd = false;
var fileStream = {
end: function(callback) {
calledEnd = true;
callback();
}
};
xunit.done.call({fileStream: fileStream}, expectedFailure, callback);
expect(calledEnd, 'to be', true);
expect(callbackArgument, 'to be', expectedFailure);
});
});
describe('if fileStream is falsy', function() {
it('should run callback with failure', function() {
var xunit = new XUnit({on: function() {}, once: function() {}});
var callback = function(failures) {
callbackArgument = failures;
};
xunit.done.call({fileStream: false}, expectedFailure, callback);
expect(callbackArgument, 'to be', expectedFailure);
});
});
});
describe('write', function() {
describe('if fileStream is truthly', function() {
it('should call fileStream write with line and new line', function() {
var xunit = new XUnit({on: function() {}, once: function() {}});
var fileStream = {
write: function(write) {
expectedWrite = write;
}
};
xunit.write.call({fileStream: fileStream}, expectedLine);
expect(expectedWrite, 'to be', expectedLine + '\n');
});
});
describe('if fileStream is falsy and stdout exists', function() {
it('should call write with line and new line', function() {
stdoutWrite = process.stdout.write;
process.stdout.write = function(string) {
stdout.push(string);
};
var xunit = new XUnit({on: function() {}, once: function() {}});
xunit.write.call({fileStream: false}, expectedLine);
process.stdout.write = stdoutWrite;
expect(stdout[0], 'to be', expectedLine + '\n');
});
});
describe('if fileStream is falsy and stdout does not exist', function() {
it('should call write with line', function() {
stdoutWrite = process;
process = false; // eslint-disable-line no-native-reassign, no-global-assign
var cachedConsoleLog = console.log;
console.log = function(string) {
stdout.push(string);
};
var xunit = new XUnit({on: function() {}, once: function() {}});
xunit.write.call({fileStream: false}, expectedLine);
console.log = cachedConsoleLog;
process = stdoutWrite; // eslint-disable-line no-native-reassign, no-global-assign
expect(stdout[0], 'to be', expectedLine);
});
});
});
describe('test', function() {
describe('on test failure', function() {
it('should write expected tag with error details', function() {
var xunit = new XUnit({on: function() {}, once: function() {}});
var expectedTest = {
state: 'failed',
title: expectedTitle,
parent: {
fullTitle: function() {
return expectedClassName;
}
},
duration: 1000,
err: {
actual: 'foo',
expected: 'bar',
message: expectedMessage,
stack: expectedStack
}
};
xunit.test.call(
{
write: function(string) {
expectedWrite = string;
}
},
expectedTest
);
var expectedTag =
'<testcase classname="' +
expectedClassName +
'" name="' +
expectedTitle +
'" time="1"><failure>' +
expectedMessage +
'\n' +
expectedDiff +
'\n' +
expectedStack +
'</failure></testcase>';
expect(expectedWrite, 'to be', expectedTag);
});
});
describe('on test pending', function() {
it('should write expected tag', function() {
var xunit = new XUnit({on: function() {}, once: function() {}});
var expectedTest = {
isPending: function() {
return true;
},
title: expectedTitle,
parent: {
fullTitle: function() {
return expectedClassName;
}
},
duration: 1000
};
xunit.test.call(
{
write: function(string) {
expectedWrite = string;
}
},
expectedTest
);
var expectedTag =
'<testcase classname="' +
expectedClassName +
'" name="' +
expectedTitle +
'" time="1"><skipped/></testcase>';
expect(expectedWrite, 'to be', expectedTag);
});
});
describe('on test in any other state', function() {
it('should write expected tag', function() {
var xunit = new XUnit({on: function() {}, once: function() {}});
var expectedTest = {
isPending: function() {
return false;
},
title: expectedTitle,
parent: {
fullTitle: function() {
return expectedClassName;
}
},
duration: false
};
xunit.test.call(
{
write: function(string) {
expectedWrite = string;
}
},
expectedTest
);
var expectedTag =
'<testcase classname="' +
expectedClassName +
'" name="' +
expectedTitle +
'" time="0"/>';
expect(expectedWrite, 'to be', expectedTag);
});
});
it('should write expected summary statistics', function() {
var count = 0;
var simpleError = {
actual: 'foo',
expected: 'bar',
message: expectedMessage,
stack: expectedStack
};
var generateTest = function(passed) {
var t = {
title: expectedTitle + count,
state: passed ? 'passed' : 'failed',
isPending: function() {
return false;
},
slow: function() {
return false;
},
parent: {
fullTitle: function() {
return expectedClassName;
}
},
duration: 1000
};
return t;
};
var runner = new EventEmitter();
createStatsCollector(runner);
var xunit = new XUnit(runner);
expectedWrite = '';
xunit.write = function(string) {
expectedWrite += string;
};
// 3 tests, no failures (i.e. tests that could not run), and 2 errors
runner.emit('test end');
runner.emit('pass', generateTest(true));
runner.emit('test end');
runner.emit('fail', generateTest(false), simpleError);
runner.emit('test end');
runner.emit('fail', generateTest(false), simpleError);
runner.emit('end');
var expectedTag =
'<testsuite name="Mocha Tests" tests="3" failures="0" errors="2" skipped="0"';
expect(expectedWrite, 'to contain', expectedTag);
expect(expectedWrite, 'to contain', '</testsuite>');
});
});
describe('custom suite name', function() {
// capture the events that the reporter subscribes to
var events;
// capture output lines (will contain the resulting XML of the xunit reporter)
var lines;
// the file stream into which the xunit reporter will write into
var fileStream;
beforeEach(function() {
events = {};
runner.on = runner.once = function(eventName, eventHandler) {
// capture the event handler
events[eventName] = eventHandler;
};
lines = [];
fileStream = {
write: function(line) {
// capture the output lines
lines.push(line);
}
};
});
it('should use "Mocha Tests" as the suite name if no custom name is provided', function() {
// arrange
var xunit = new XUnit(runner);
xunit.fileStream = fileStream;
// act (trigger the end event to force xunit reporter to write the output)
events['end']();
// assert
assert(
lines[0].indexOf('Mocha Tests') >= 0,
'it should contain the text "Mocha Tests"'
);
});
it('should use the custom suite name as the suite name when provided in the reporter options', function() {
// arrange
var options = {
reporterOptions: {
// this time, with a custom suite name
suiteName: 'Mocha Is Great!'
}
};
var xunit = new XUnit(runner, options);
xunit.fileStream = fileStream;
// act (trigger the end event to force xunit reporter to write the output)
events['end']();
// assert
assert(
lines[0].indexOf('<testsuite name="Mocha Is Great!"') === 0,
'"' + lines[0] + '" should contain the text "Mocha Is Great"'
);
});
});
});