-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
xunit.spec.js
595 lines (509 loc) · 16.3 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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
'use strict';
var EventEmitter = require('events').EventEmitter;
var fs = require('fs');
var path = require('path');
var sinon = require('sinon');
var createStatsCollector = require('../../lib/stats-collector');
var events = require('../../').Runner.constants;
var reporters = require('../../').reporters;
var states = require('../../').Runnable.constants;
const {createTempDir, touchFile} = require('../integration/helpers');
var Base = reporters.Base;
var XUnit = reporters.XUnit;
var EVENT_RUN_END = events.EVENT_RUN_END;
var EVENT_TEST_END = events.EVENT_TEST_END;
var EVENT_TEST_FAIL = events.EVENT_TEST_FAIL;
var EVENT_TEST_PASS = events.EVENT_TEST_PASS;
var EVENT_TEST_PENDING = events.EVENT_TEST_PENDING;
var STATE_FAILED = states.STATE_FAILED;
var STATE_PASSED = states.STATE_PASSED;
describe('XUnit reporter', function () {
var runner;
var noop = function () {};
var expectedLine = 'some-line';
var expectedClassName = 'fullTitle';
var expectedTitle = 'some title';
var expectedFile = 'testFile.spec.js';
var expectedMessage = 'some message';
var expectedDiff =
'\n + expected - actual\n\n -foo\n +bar\n ';
var expectedStack = 'some-stack';
beforeEach(function () {
runner = {on: noop, once: noop};
createStatsCollector(runner);
});
describe("when 'reporterOptions.output' is provided", function () {
var expectedOutput = path.join(path.sep, 'path', 'to', 'some-output');
var options = {
reporterOptions: {
output: expectedOutput
}
};
describe('when fileStream can be created', function () {
var fsMkdirSync;
var fsCreateWriteStream;
beforeEach(function () {
fsMkdirSync = sinon.stub(fs, 'mkdirSync');
fsCreateWriteStream = sinon.stub(fs, 'createWriteStream');
});
it('should open given file for writing, recursively creating directories in pathname', function () {
var fakeThis = {
fileStream: null
};
XUnit.call(fakeThis, runner, options);
var expectedDirectory = path.dirname(expectedOutput);
expect(
fsMkdirSync.calledWith(expectedDirectory, {
recursive: true
}),
'to be true'
);
expect(fsCreateWriteStream.calledWith(expectedOutput), 'to be true');
});
afterEach(function () {
sinon.restore();
});
});
describe('when fileStream cannot be created', function () {
describe('when given an invalid pathname', function () {
/**
* @type {string}
*/
let tmpdir;
/**
* @type {import('../integration/helpers').RemoveTempDirCallback}
*/
let cleanup;
var invalidPath;
beforeEach(async function () {
const {dirpath, removeTempDir} = await createTempDir();
tmpdir = dirpath;
cleanup = removeTempDir;
// Create path where file 'some-file' used as directory
invalidPath = path.join(
tmpdir,
'some-file',
path.basename(expectedOutput)
);
touchFile(path.dirname(invalidPath));
});
it('should throw system error', function () {
var options = {
reporterOptions: {
output: invalidPath
}
};
var boundXUnit = XUnit.bind({}, runner, options);
expect(
boundXUnit,
'to throw',
expect.it('to be an', Error).and('to satisfy', {
syscall: 'mkdir',
code: 'EEXIST',
path: path.dirname(invalidPath)
})
);
});
afterEach(function () {
cleanup();
});
});
describe('when run in browser', function () {
beforeEach(function () {
sinon.stub(fs, 'createWriteStream').value(false);
});
it('should throw unsupported error', function () {
var boundXUnit = XUnit.bind({}, runner, options);
expect(
boundXUnit,
'to throw',
'file output not supported in browser'
);
});
afterEach(function () {
sinon.restore();
});
});
});
});
describe('event handlers', function () {
describe("on 'pending', 'pass' and 'fail' events", function () {
it("should add test to tests called on 'end' event", function () {
var pendingTest = {
name: 'pending',
slow: noop
};
var failTest = {
name: 'fail',
slow: noop
};
var passTest = {
name: 'pass',
slow: noop
};
runner.on = runner.once = function (event, callback) {
if (event === EVENT_TEST_PENDING) {
callback(pendingTest);
} else if (event === EVENT_TEST_PASS) {
callback(passTest);
} else if (event === EVENT_TEST_FAIL) {
callback(failTest);
} else if (event === EVENT_RUN_END) {
callback();
}
};
var calledTests = [];
var fakeThis = {
write: noop,
test: function (test) {
calledTests.push(test);
}
};
XUnit.call(fakeThis, runner);
var expectedCalledTests = [pendingTest, passTest, failTest];
expect(calledTests, 'to equal', expectedCalledTests);
});
});
});
describe('#done', function () {
var xunit;
var options = {
reporterOptions: {}
};
var expectedNFailures = 13;
var callback;
beforeEach(function () {
callback = sinon.spy();
});
afterEach(function () {
callback = null;
xunit = null;
sinon.restore();
});
describe('when output directed to file', function () {
var fakeThis;
beforeEach(function () {
xunit = new XUnit(runner, options);
fakeThis = {
fileStream: {
end: sinon.stub().callsFake(function (chunk, encoding, cb) {
if (typeof arguments[0] === 'function') {
cb = arguments[0];
}
cb();
}),
write: function (chunk, encoding, cb) {}
}
};
});
it("should run completion callback via 'fileStream.end'", function () {
xunit.done.call(fakeThis, expectedNFailures, callback);
expect(fakeThis.fileStream.end.calledOnce, 'to be true');
expect(callback.calledOnce, 'to be true');
expect(callback.calledWith(expectedNFailures), 'to be true');
});
});
describe('when output directed to stdout (or console)', function () {
var fakeThis;
beforeEach(function () {
xunit = new XUnit(runner, options);
fakeThis = {};
});
it('should run completion callback', function () {
xunit.done.call(fakeThis, expectedNFailures, callback);
expect(callback.calledOnce, 'to be true');
expect(callback.calledWith(expectedNFailures), 'to be true');
});
});
});
describe('#write', function () {
// :TODO: Method should be named 'writeln', not 'write'
describe('when output directed to file', function () {
var fileStream = {
write: sinon.spy()
};
it("should call 'fileStream.write' with line and newline", function () {
var xunit = new XUnit(runner);
var fakeThis = {fileStream};
xunit.write.call(fakeThis, expectedLine);
expect(fileStream.write.calledWith(expectedLine + '\n'), 'to be true');
});
});
describe('when output directed to stdout', function () {
it("should call 'process.stdout.write' with line and newline", function () {
var xunit = new XUnit(runner);
var fakeThis = {fileStream: false};
var stdoutWriteStub = sinon.stub(process.stdout, 'write');
xunit.write.call(fakeThis, expectedLine);
stdoutWriteStub.restore();
expect(stdoutWriteStub.calledWith(expectedLine + '\n'), 'to be true');
});
});
describe('when output directed to console', function () {
it("should call 'Base.consoleLog' with line", function () {
// :TODO: XUnit needs a trivially testable means to force console.log()
var realProcess = process;
process = false; // eslint-disable-line no-native-reassign, no-global-assign
var xunit = new XUnit(runner);
var fakeThis = {fileStream: false};
var consoleLogStub = sinon.stub(Base, 'consoleLog');
xunit.write.call(fakeThis, expectedLine);
consoleLogStub.restore();
process = realProcess; // eslint-disable-line no-native-reassign, no-global-assign
expect(consoleLogStub.calledWith(expectedLine), 'to be true');
});
});
});
describe('#test', function () {
var expectedWrite;
var fakeThis = {
write: function (str) {
expectedWrite = str;
}
};
beforeEach(function () {
sinon.stub(Base, 'useColors').value(false);
});
afterEach(function () {
sinon.restore();
expectedWrite = null;
});
describe('on test failure', function () {
it('should write expected tag with error details', function () {
var xunit = new XUnit(runner);
var expectedTest = {
state: STATE_FAILED,
title: expectedTitle,
file: expectedFile,
parent: {
fullTitle: function () {
return expectedClassName;
}
},
duration: 1000,
err: {
actual: 'foo',
expected: 'bar',
message: expectedMessage,
stack: expectedStack
}
};
xunit.test.call(fakeThis, expectedTest);
sinon.restore();
var expectedTag =
'<testcase classname="' +
expectedClassName +
'" name="' +
expectedTitle +
'" file="' +
expectedFile +
'" time="1"><failure>' +
expectedMessage +
'\n' +
expectedDiff +
'\n' +
expectedStack +
'</failure></testcase>';
expect(expectedWrite, 'to be', expectedTag);
});
it('should handle non-string diff values', function () {
var runner = new EventEmitter();
createStatsCollector(runner);
var xunit = new XUnit(runner);
var expectedTest = {
state: STATE_FAILED,
title: expectedTitle,
file: expectedFile,
parent: {
fullTitle: function () {
return expectedClassName;
}
},
duration: 1000,
err: {
actual: 1,
expected: 2,
message: expectedMessage,
stack: expectedStack
}
};
sinon.stub(xunit, 'write').callsFake(function (str) {
expectedWrite += str;
});
runner.emit(EVENT_TEST_FAIL, expectedTest, expectedTest.err);
runner.emit(EVENT_RUN_END);
sinon.restore();
var expectedDiff =
'\n + expected - actual\n\n -1\n +2\n ';
expect(expectedWrite, 'to contain', expectedDiff);
});
});
describe('on test pending', function () {
it('should write expected tag', function () {
var xunit = new XUnit(runner);
var expectedTest = {
isPending: function () {
return true;
},
title: expectedTitle,
file: expectedFile,
parent: {
fullTitle: function () {
return expectedClassName;
}
},
duration: 1000
};
xunit.test.call(fakeThis, expectedTest);
sinon.restore();
var expectedTag =
'<testcase classname="' +
expectedClassName +
'" name="' +
expectedTitle +
'" file="' +
expectedFile +
'" 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(runner);
var expectedTest = {
isPending: function () {
return false;
},
title: expectedTitle,
file: expectedFile,
parent: {
fullTitle: function () {
return expectedClassName;
}
},
duration: false
};
xunit.test.call(fakeThis, expectedTest);
sinon.restore();
var expectedTag =
'<testcase classname="' +
expectedClassName +
'" name="' +
expectedTitle +
'" file="' +
expectedFile +
'" time="0"/>';
expect(expectedWrite, 'to be', expectedTag);
});
});
it('should write expected summary statistics', function () {
var numTests = 0;
var numPass = 0;
var numFail = 0;
var simpleError = {
actual: 'foo',
expected: 'bar',
message: expectedMessage,
stack: expectedStack
};
var generateTest = function (passed) {
numTests++;
if (passed) {
numPass++;
} else {
numFail++;
}
return {
title: [expectedTitle, numTests].join(': '),
state: passed ? STATE_PASSED : STATE_FAILED,
isPending: function () {
return false;
},
slow: function () {
return false;
},
parent: {
fullTitle: function () {
return expectedClassName;
}
},
duration: 1000
};
};
var runner = new EventEmitter();
createStatsCollector(runner);
var xunit = new XUnit(runner);
expectedWrite = '';
sinon.stub(xunit, 'write').callsFake(function (str) {
expectedWrite += str;
});
// 3 tests, no failures (i.e. tests that could not run), and 2 errors
runner.emit(EVENT_TEST_PASS, generateTest(true));
runner.emit(EVENT_TEST_END);
runner.emit(EVENT_TEST_FAIL, generateTest(false), simpleError);
runner.emit(EVENT_TEST_END);
runner.emit(EVENT_TEST_FAIL, generateTest(false), simpleError);
runner.emit(EVENT_TEST_END);
runner.emit(EVENT_RUN_END);
sinon.restore();
var expectedNumPass = 1;
var expectedNumFail = 2;
var expectedNumTests = 3;
expect(expectedNumPass, 'to be', numPass);
expect(expectedNumFail, 'to be', numFail);
expect(expectedNumTests, 'to be', numTests);
// :NOTE: Mocha test "fail" is an XUnit "error"
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('suite name', function () {
// Capture the events that the reporter subscribes to
var events = {};
// Capture output lines (will contain the resulting XML of XUnit reporter)
var lines = [];
// File stream into which the XUnit reporter will write
var fileStream;
before(function () {
fileStream = {
write: function (chunk, encoding, cb) {
lines.push(chunk);
}
};
});
beforeEach(function () {
lines = [];
events = {};
runner.on = runner.once = function (eventName, eventHandler) {
// Capture the event handler
events[eventName] = eventHandler;
};
});
it('should use custom name if provided via reporter options', function () {
var customSuiteName = 'Mocha Is Great!';
var options = {
reporterOptions: {
suiteName: customSuiteName
}
};
var xunit = new XUnit(runner, options);
xunit.fileStream = fileStream;
// Trigger end event to force XUnit reporter to write its output
events[EVENT_RUN_END]();
expect(lines[0], 'to contain', customSuiteName);
});
it('should use default name otherwise', function () {
var defaultSuiteName = 'Mocha Tests';
var options = {
reporterOptions: {}
};
var xunit = new XUnit(runner, options);
xunit.fileStream = fileStream;
// Trigger end event to force XUnit reporter to write its output
events[EVENT_RUN_END]();
expect(lines[0], 'to contain', defaultSuiteName);
});
});
});