-
Notifications
You must be signed in to change notification settings - Fork 18
/
MochaAdapter.js
190 lines (168 loc) · 5.14 KB
/
MochaAdapter.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
const EventEmitter = require('events');
const helpers = require('../helpers.js');
module.exports = class MochaAdapter extends EventEmitter {
constructor (mocha) {
super();
this.errors = null;
this.finalRuntime = 0;
this.finalCounts = {
passed: 0,
failed: 0,
skipped: 0,
todo: 0,
total: 0
};
// Mocha will instantiate the given function as a class, even if you only need a callback.
// As such, it can't be an arrow function as those throw TypeError when instantiated.
const self = this;
mocha.reporter(function (runner) {
self.runner = runner;
runner.on('start', self.onStart.bind(self));
runner.on('suite', self.onSuite.bind(self));
runner.on('test', self.onTest.bind(self));
runner.on('pending', self.onPending.bind(self));
runner.on('fail', self.onFail.bind(self));
runner.on('test end', self.onTestEnd.bind(self));
runner.on('suite end', self.onSuiteEnd.bind(self));
runner.on('end', self.onEnd.bind(self));
});
}
convertToSuiteStart (mochaSuite) {
return {
name: mochaSuite.title,
fullName: this.titlePath(mochaSuite)
};
}
convertToSuiteEnd (mochaSuite) {
const tests = mochaSuite.tests.map(this.convertTest.bind(this));
const childSuites = mochaSuite.suites.map(this.convertToSuiteEnd.bind(this));
const helperData = helpers.aggregateTests([...tests, ...childSuites]);
return {
name: mochaSuite.title,
fullName: this.titlePath(mochaSuite),
status: helperData.status,
runtime: helperData.runtime
};
}
convertTest (mochaTest) {
let suiteName;
let fullName;
if (!mochaTest.parent.root) {
suiteName = mochaTest.parent.title;
fullName = this.titlePath(mochaTest.parent);
// Add also the test name.
fullName.push(mochaTest.title);
} else {
suiteName = null;
fullName = [mochaTest.title];
}
if (mochaTest.errors !== undefined) {
// If the test has the 'errors' property, this is a "test end".
const errors = mochaTest.errors.map((error) => ({
passed: false,
actual: error.actual,
expected: error.expected,
message: error.message || error.toString(),
stack: error.stack
}));
// Mocha 8.0 introduced STATE_PENDING
// https://github.com/qunitjs/js-reporters/issues/116
const status = (mochaTest.state === undefined || mochaTest.state === 'pending') ? 'skipped' : mochaTest.state;
const runtime = (mochaTest.duration === undefined) ? null : mochaTest.duration;
return {
name: mochaTest.title,
suiteName,
fullName,
status,
runtime,
errors,
assertions: errors
};
} else {
// It is a "test start".
return {
name: mochaTest.title,
suiteName,
fullName
};
}
}
titlePath (mochaSuite) {
if (mochaSuite.titlePath) {
// Mocha 4.0+ has Suite#titlePath()
return mochaSuite.titlePath();
}
const fullName = [];
if (!mochaSuite.root) {
fullName.push(mochaSuite.title);
}
let parent = mochaSuite.parent;
while (parent && !parent.root) {
fullName.unshift(parent.title);
parent = parent.parent;
}
return fullName;
}
onStart () {
// total is all tests + all suites
// each suite gets a CRI "test" wrapper
let total = this.runner.suite.total();
this.runner.suite.suites.forEach(function addSuites (suite) {
total++;
suite.suites.forEach(addSuites);
});
this.emit('runStart', {
name: null,
testCounts: {
total: total
}
});
}
onSuite (mochaSuite) {
if (!mochaSuite.root) {
this.emit('suiteStart', this.convertToSuiteStart(mochaSuite));
}
}
onTest (mochaTest) {
this.errors = [];
this.emit('testStart', this.convertTest(mochaTest));
}
/**
* Mocha emits skipped tests here instead of on the "test" event.
*/
onPending (mochaTest) {
this.emit('testStart', this.convertTest(mochaTest));
}
onFail (test, error) {
this.errors.push(error);
}
onTestEnd (mochaTest) {
// Save the errors on Mocha's test object, because when the suite that
// contains this test is emitted on the "suiteEnd" event, it should also
// contain this test with all its details (errors, status, runtime). Runtime
// and status are already attached to the test, but the errors are not.
mochaTest.errors = this.errors;
const testEnd = this.convertTest(mochaTest);
this.emit('testEnd', testEnd);
this.finalCounts.total++;
this.finalCounts[testEnd.status]++;
this.finalRuntime += testEnd.runtime || 0;
}
onSuiteEnd (mochaSuite) {
if (!mochaSuite.root) {
const suiteEnd = this.convertToSuiteEnd(mochaSuite);
this.emit('suiteEnd', suiteEnd);
this.finalCounts.total++;
this.finalCounts[suiteEnd.status]++;
this.finalRuntime += suiteEnd.runtime || 0;
}
}
onEnd (details) {
this.emit('runEnd', {
name: null,
status: this.finalCounts.failed > 0 ? 'failed' : 'passed',
testCounts: this.finalCounts,
runtime: this.finalRuntime
});
}
};