-
Notifications
You must be signed in to change notification settings - Fork 18
/
JasmineAdapter.js
172 lines (149 loc) · 4.93 KB
/
JasmineAdapter.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
const EventEmitter = require('events');
const helpers = require('../helpers.js');
/**
* Known limitations:
*
* - Errors in afterAll are ignored.
*/
module.exports = class JasmineAdapter extends EventEmitter {
constructor (jasmine) {
super();
// NodeJS or browser
this.env = jasmine.env || jasmine.getEnv();
this.suiteChildren = {};
this.suiteEnds = [];
this.suiteStarts = {};
this.testStarts = {};
this.testEnds = {};
// See <https://jasmine.github.io/api/3.6/Reporter.html>
const reporter = {
jasmineStarted: this.onJasmineStarted.bind(this),
specDone: this.onSpecDone.bind(this),
specStarted: this.onSpecStarted.bind(this),
suiteStarted: this.onSuiteStarted.bind(this),
suiteDone: this.onSuiteDone.bind(this),
jasmineDone: this.onJasmineDone.bind(this)
};
if (jasmine.addReporter) {
// For Node.js, use the method from jasmine-npm
jasmine.addReporter(reporter);
} else {
// For browser, use the method from jasmine-core
this.env.addReporter(reporter);
}
}
createAssertion (expectation) {
return {
passed: expectation.passed,
actual: expectation.actual,
expected: expectation.expected,
message: expectation.message,
stack: expectation.stack !== '' ? expectation.stack : null
};
}
createTestEnd (testStart, result) {
const errors = result.failedExpectations.map((expectation) => this.createAssertion(expectation));
const assertions = errors.concat(
result.passedExpectations.map((expectation) => this.createAssertion(expectation))
);
return {
name: testStart.name,
suiteName: testStart.suiteName,
fullName: testStart.fullName.slice(),
status: (result.status === 'pending') ? 'skipped' : result.status,
// TODO: Jasmine 3.4+ has result.duration, use it.
// Note that result.duration uses 0 instead of null for a 'skipped' test.
runtime: (result.status === 'pending') ? null : (new Date() - this.startTime),
errors,
assertions
};
}
/**
* Traverse the Jasmine structured returned by `this.env.topSuite()`
* in order to extract the child-parent relations and full names.
*
*/
processSuite (result, parentNames, parentIds) {
const isGlobalSuite = (result.description === 'Jasmine__TopLevel__Suite');
const name = isGlobalSuite ? null : result.description;
const fullName = parentNames.slice();
if (!isGlobalSuite) {
fullName.push(name);
}
parentIds.push(result.id);
this.suiteChildren[result.id] = [];
result.children.forEach((child) => {
if (child.id.indexOf('suite') === 0) {
this.suiteStarts[child.id] = {
name: child.description,
fullName: [...fullName, child.description]
};
this.processSuite(child, fullName.slice(), parentIds.slice());
} else {
this.testStarts[child.id] = {
name: child.description,
suiteName: name,
fullName: [...fullName, child.description]
};
// Update flat list of test children
parentIds.forEach((id) => {
this.suiteChildren[id].push(child.id);
});
}
});
}
createSuiteEnd (testStart, result) {
const tests = this.suiteChildren[result.id].map((testId) => this.testEnds[testId]);
const helperData = helpers.aggregateTests(tests);
return {
name: testStart.name,
fullName: testStart.fullName,
// Jasmine has result.status, but does not propagate 'todo' or 'skipped'
status: helperData.status,
runtime: result.duration || helperData.runtime
};
}
onJasmineStarted () {
this.processSuite(this.env.topSuite(), [], []);
let total = 0;
this.env.topSuite().children.forEach(function countChild (child) {
total++;
if (child.id.indexOf('suite') === 0) {
child.children.forEach(countChild);
}
});
this.emit('runStart', {
name: null,
testCounts: {
total: total
}
});
}
onSuiteStarted (result) {
this.emit('suiteStart', this.suiteStarts[result.id]);
}
onSpecStarted (result) {
this.startTime = new Date();
this.emit('testStart', this.testStarts[result.id]);
}
onSpecDone (result) {
this.testEnds[result.id] = this.createTestEnd(this.testStarts[result.id], result);
this.emit('testEnd', this.testEnds[result.id]);
}
onSuiteDone (result) {
const suiteEnd = this.createSuiteEnd(this.suiteStarts[result.id], result);
this.suiteEnds.push(suiteEnd);
this.emit('suiteEnd', suiteEnd);
}
onJasmineDone (doneInfo) {
const topSuite = this.env.topSuite();
const tests = this.suiteChildren[topSuite.id].map((testId) => this.testEnds[testId]);
const helperData = helpers.aggregateTests([...tests, ...this.suiteEnds]);
this.emit('runEnd', {
name: null,
status: helperData.status,
testCounts: helperData.testCounts,
runtime: helperData.runtime
});
}
};