-
Notifications
You must be signed in to change notification settings - Fork 159
/
reporter.test.js
440 lines (384 loc) · 12.3 KB
/
reporter.test.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
const Mocha = require('mocha');
const createStatsCollector = require('mocha/lib/stats-collector');
const sinon = require('sinon');
const proxyquire = require('proxyquire');
const Assert = require('assert').AssertionError;
const utils = require('../src/utils');
const { Runner, Suite, Test } = Mocha;
const makeTest = (title, doneFn) => new Test(title, doneFn);
const reportStub = sinon.stub();
const logStub = sinon.stub();
const specStub = sinon.stub();
const nyanStub = sinon.stub();
const statsCollectorStub = sinon.stub();
utils.log = logStub;
const baseConfig = {
quiet: false,
reportFilename: 'mochawesome',
saveHtml: true,
saveJson: true,
useInlineDiffs: false,
consoleReporter: 'spec',
code: true,
};
const mochawesome = proxyquire('../src/mochawesome', {
'mochawesome-report-generator': {
create: reportStub,
},
'mocha/lib/reporters/spec': specStub,
'mocha/lib/reporters/nyan': nyanStub,
'mocha/lib/stats-collector': statsCollectorStub,
'./utils': utils,
});
describe('Mochawesome Reporter', () => {
let mocha;
let suite;
let subSuite;
let runner;
let mochaReporter;
beforeEach(() => {
mocha = new Mocha({ reporter: mochawesome });
suite = new Suite('', 'root');
subSuite = new Suite('Mochawesome Suite', 'root');
suite.addSuite(subSuite);
runner = new Runner(suite);
// Overwrite runner's `uncaughtEnd` function to prevent
// mocha exiting the run on a test failure
runner.uncaughtEnd = () => {};
createStatsCollector(runner);
mochaReporter = new mocha._reporter(runner, {
reporterOptions: {
quiet: true,
},
});
});
describe('Stats Collector', () => {
beforeEach(() => {
delete runner.stats;
mochaReporter = new mocha._reporter(runner, {
reporterOptions: {
quiet: true,
},
});
});
it('should initialize stats collector', () => {
statsCollectorStub.called.should.equal(true);
});
});
describe('Test Handling', () => {
it('should have 1 test passing', done => {
const test = makeTest('passing test', () => {});
subSuite.addTest(test);
runner.run(failureCount => {
failureCount.should.equal(0);
mochaReporter.stats.passPercent.should.equal(100);
done();
});
});
it('should have 1 test failure', done => {
const error = { expected: { a: 1 }, actual: { a: 2 } };
const test = makeTest('failing test', tDone => tDone(new Assert(error)));
subSuite.addTest(test);
runner.run(failureCount => {
failureCount.should.equal(1);
mochaReporter.stats.passPercent.should.equal(0);
done();
});
});
it('should have 1 test pending', done => {
const test = makeTest('pending test');
subSuite.addTest(test);
runner.run(failureCount => {
failureCount.should.equal(0);
mochaReporter.stats.pending.should.equal(1);
mochaReporter.stats.pendingPercent.should.equal(100);
done();
});
});
it('should have a mix of tests', done => {
const error = { expected: 'foo', actual: 'bar' };
const passTest1 = makeTest('pass1', () => {});
const passTest2 = makeTest('pass2', () => {});
const passTest3 = makeTest('pass3', () => {});
const failTest = makeTest('failing test', tDone =>
tDone(new Assert(error))
);
[passTest1, passTest2, passTest3, failTest].forEach(test =>
subSuite.addTest(test)
);
runner.run(() => {
mochaReporter.stats.passes.should.equal(3);
mochaReporter.stats.failures.should.equal(1);
mochaReporter.stats.passPercent.should.equal(75);
done();
});
});
it('should handle empty suite', done => {
runner.run(failureCount => {
failureCount.should.equal(0);
done();
});
});
it('should handle root suite with file', done => {
const test = makeTest('test', () => {});
test.file = 'testfile.js';
test.fullFile = 'testfile.js';
suite.addTest(test);
suite.suites = [];
runner.run(() => {
mochaReporter.output.results[0].fullFile.should.equal('testfile.js');
done();
});
});
it('should handle suite with file', done => {
const test = makeTest('test', () => {});
subSuite.addTest(test);
subSuite.file = 'testfile.js';
runner.run(() => {
mochaReporter.output.results[0].suites[0].fullFile.should.equal(
'testfile.js'
);
done();
});
});
});
describe('Hook Handling', () => {
function passingHookTest(hookType, isBefore) {
it(`${hookType} passing hook`, done => {
const test = makeTest('passing test', () => {});
subSuite[hookType](`${hookType} passing hook`, () => {});
subSuite.addTest(test);
runner.run(() => {
const testSuite = mochaReporter.output.results[0].suites[0];
const { beforeHooks, afterHooks } = testSuite;
afterHooks.length.should.equal(isBefore ? 0 : 1);
beforeHooks.length.should.equal(isBefore ? 1 : 0);
done();
});
});
}
function failingHookTest(hookType, isBefore) {
it(`${hookType} failing hook`, done => {
const test = makeTest('passing test', () => {});
subSuite[hookType](`${hookType} failing hook`, () => {
throw new Error('Dummy hook error');
});
subSuite.addTest(test);
runner.run(() => {
const testSuite = mochaReporter.output.results[0].suites[0];
const { beforeHooks, afterHooks } = testSuite;
afterHooks.length.should.equal(isBefore ? 0 : 1);
beforeHooks.length.should.equal(isBefore ? 1 : 0);
done();
});
});
}
['beforeAll', 'beforeEach'].forEach(type => {
passingHookTest(type, true);
failingHookTest(type, true);
});
['afterAll', 'afterEach'].forEach(type => {
passingHookTest(type, false);
failingHookTest(type, false);
});
});
describe('Options Handling', () => {
const makeReporter = opts => new mocha._reporter(runner, opts);
const expected = opts => ({ ...baseConfig, ...opts });
beforeEach(() => {
subSuite.addTest(makeTest('test', () => {}));
});
describe('environment variables', () => {
beforeEach(() => {
process.env.MOCHAWESOME_REPORTFILENAME = 'test';
mochaReporter = makeReporter({});
});
afterEach(() => {
delete process.env.MOCHAWESOME_REPORTFILENAME;
});
it('should apply reporter options via environment variables', done => {
runner.run(() => {
mochaReporter.config.should.deepEqual(
expected({
reportFilename: 'test',
})
);
done();
});
});
});
describe('options object', () => {
beforeEach(() => {
process.env.MOCHAWESOME_QUIET = 'false';
mochaReporter = makeReporter({
reporterOptions: {
reportFilename: 'testReportFilename',
json: 'false',
margeSpecificOption: 'something',
},
});
});
it('should apply reporter options via passed in object', done => {
runner.run(() => {
mochaReporter.config.should.deepEqual(
expected({
reportFilename: 'testReportFilename',
saveJson: false,
})
);
done();
});
});
});
describe('mocha options', () => {
beforeEach(() => {
mochaReporter = makeReporter({ inlineDiffs: true });
});
it('should transfer mocha options', done => {
runner.run(() => {
mochaReporter.config.should.deepEqual(
expected({
useInlineDiffs: true,
})
);
done();
});
});
});
describe('console reporter', () => {
it('should default to spec console reporter', done => {
specStub.reset();
mochaReporter = makeReporter({
reporterOptions: {
consoleReporter: 'unknown',
},
});
runner.run(() => {
specStub.called.should.equal(true);
mochaReporter.config.should.deepEqual(
expected({
consoleReporter: 'unknown',
})
);
done();
});
});
it('should allow overriding the console reporter', done => {
nyanStub.reset();
mochaReporter = makeReporter({
reporterOptions: {
consoleReporter: 'nyan',
},
});
runner.run(() => {
nyanStub.called.should.equal(true);
mochaReporter.config.should.deepEqual(
expected({
consoleReporter: 'nyan',
})
);
done();
});
});
it('should disable the console reporter with the none reporter', done => {
specStub.reset();
mochaReporter = makeReporter({
reporterOptions: {
consoleReporter: 'none',
},
});
runner.run(() => {
specStub.called.should.equal(false);
mochaReporter.config.should.deepEqual(
expected({
consoleReporter: 'none',
})
);
done();
});
});
});
});
describe('Reporter Done Function', () => {
let mochaExitFn;
beforeEach(() => {
subSuite.addTest(makeTest('test', () => {}));
mochaExitFn = sinon.spy();
logStub.reset();
reportStub.reset();
mochaReporter = new mocha._reporter(runner, {
reporterOptions: {
reportDir: 'testDir',
inlineAssets: true,
quiet: true,
},
});
});
it('should not have an unhandled error', () => {
reportStub.returns(Promise.resolve({}));
return mochaReporter.done(0).then(() => {
mochaExitFn.called.should.equal(false);
logStub.neverCalledWith('error').should.equal(true);
});
});
it('should log message when no files generated', () => {
reportStub.resolves([]);
return mochaReporter.done(0, mochaExitFn).then(() => {
mochaExitFn.args[0][0].should.equal(0);
logStub.args[0][0].should.equal('No files were generated');
});
});
it('should log message when only html file generated', () => {
reportStub.resolves(['html', null]);
return mochaReporter.done(0, mochaExitFn).then(() => {
mochaExitFn.args[0][0].should.equal(0);
logStub.callCount.should.equal(1);
logStub.args[0][0].should.equal('Report HTML saved to html');
});
});
it('should log message when only json file generated', () => {
reportStub.resolves([null, 'json']);
return mochaReporter.done(0, mochaExitFn).then(() => {
mochaExitFn.args[0][0].should.equal(0);
logStub.callCount.should.equal(1);
logStub.args[0][0].should.equal('Report JSON saved to json');
});
});
it('should log message when html and json files generated', () => {
reportStub.resolves(['html', 'json']);
return mochaReporter.done(0, mochaExitFn).then(() => {
mochaExitFn.args[0][0].should.equal(0);
logStub.callCount.should.equal(2);
});
});
it('should pass reporterOptions to the report generator', () => {
reportStub.resolves([]);
return mochaReporter.done(0, mochaExitFn).then(() => {
reportStub.args[0][1].should.deepEqual({
reportDir: 'testDir',
inlineAssets: true,
quiet: true,
reportFilename: 'mochawesome',
saveHtml: true,
saveJson: true,
});
});
});
it('should log an error when report creation fails', () => {
reportStub.rejects({ message: 'report creation failed' });
return mochaReporter.done(0, mochaExitFn).then(() => {
mochaExitFn.called.should.equal(true);
mochaExitFn.args[0][0].should.equal(0);
logStub.called.should.equal(true);
logStub.args[0][1].should.equal('error');
});
});
it('should not log when quiet option is true', () => {
reportStub.resolves([]);
return mochaReporter.done(0, mochaExitFn).then(() => {
mochaExitFn.args[0][0].should.equal(0);
logStub.args[0][2].should.have.property('quiet', true);
});
});
});
});