Spec.js
6.45 KB
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
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
exports.default = void 0;
var _assert = require('assert');
var _ExpectationFailed = _interopRequireDefault(
require('../ExpectationFailed')
);
var _assertionErrorMessage = _interopRequireDefault(
require('../assertionErrorMessage')
);
var _expectationResultFactory = _interopRequireDefault(
require('../expectationResultFactory')
);
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {default: obj};
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
class Spec {
static isPendingSpecException(e) {
return !!(
e &&
e.toString &&
e.toString().indexOf(Spec.pendingSpecExceptionMessage) !== -1
);
}
constructor(attrs) {
_defineProperty(this, 'id', void 0);
_defineProperty(this, 'description', void 0);
_defineProperty(this, 'resultCallback', void 0);
_defineProperty(this, 'queueableFn', void 0);
_defineProperty(this, 'beforeAndAfterFns', void 0);
_defineProperty(this, 'userContext', void 0);
_defineProperty(this, 'onStart', void 0);
_defineProperty(this, 'getSpecName', void 0);
_defineProperty(this, 'queueRunnerFactory', void 0);
_defineProperty(this, 'throwOnExpectationFailure', void 0);
_defineProperty(this, 'initError', void 0);
_defineProperty(this, 'result', void 0);
_defineProperty(this, 'disabled', void 0);
_defineProperty(this, 'currentRun', void 0);
_defineProperty(this, 'markedTodo', void 0);
_defineProperty(this, 'markedPending', void 0);
_defineProperty(this, 'expand', void 0);
this.resultCallback = attrs.resultCallback || function () {};
this.id = attrs.id;
this.description = attrs.description || '';
this.queueableFn = attrs.queueableFn;
this.beforeAndAfterFns =
attrs.beforeAndAfterFns ||
function () {
return {
befores: [],
afters: []
};
};
this.userContext =
attrs.userContext ||
function () {
return {};
};
this.onStart = attrs.onStart || function () {};
this.getSpecName =
attrs.getSpecName ||
function () {
return '';
};
this.queueRunnerFactory = attrs.queueRunnerFactory || function () {};
this.throwOnExpectationFailure = !!attrs.throwOnExpectationFailure;
this.initError = new Error();
this.initError.name = ''; // Without this line v8 stores references to all closures
// in the stack in the Error object. This line stringifies the stack
// property to allow garbage-collecting objects on the stack
// https://crbug.com/v8/7142
this.initError.stack = this.initError.stack;
this.queueableFn.initError = this.initError; // @ts-expect-error
this.result = {
id: this.id,
description: this.description,
fullName: this.getFullName(),
failedExpectations: [],
passedExpectations: [],
pendingReason: '',
testPath: attrs.getTestPath()
};
}
addExpectationResult(passed, data, isError) {
const expectationResult = (0, _expectationResultFactory.default)(
data,
this.initError
);
if (passed) {
this.result.passedExpectations.push(expectationResult);
} else {
this.result.failedExpectations.push(expectationResult);
if (this.throwOnExpectationFailure && !isError) {
throw new _ExpectationFailed.default();
}
}
}
execute(onComplete, enabled) {
const self = this;
this.onStart(this);
if (
!this.isExecutable() ||
this.markedPending ||
this.markedTodo ||
enabled === false
) {
complete(enabled);
return;
}
const fns = this.beforeAndAfterFns();
const allFns = fns.befores.concat(this.queueableFn).concat(fns.afters);
this.currentRun = this.queueRunnerFactory({
queueableFns: allFns,
onException() {
// @ts-expect-error
self.onException.apply(self, arguments);
},
userContext: this.userContext(),
setTimeout,
clearTimeout,
fail: () => {}
});
this.currentRun.then(() => complete(true));
function complete(enabledAgain) {
self.result.status = self.status(enabledAgain);
self.resultCallback(self.result);
if (onComplete) {
onComplete();
}
}
}
cancel() {
if (this.currentRun) {
this.currentRun.cancel();
}
}
onException(error) {
if (Spec.isPendingSpecException(error)) {
this.pend(extractCustomPendingMessage(error));
return;
}
if (error instanceof _ExpectationFailed.default) {
return;
}
this.addExpectationResult(
false,
{
matcherName: '',
passed: false,
expected: '',
actual: '',
error: this.isAssertionError(error)
? (0, _assertionErrorMessage.default)(error, {
expand: this.expand
})
: error
},
true
);
}
disable() {
this.disabled = true;
}
pend(message) {
this.markedPending = true;
if (message) {
this.result.pendingReason = message;
}
}
todo() {
this.markedTodo = true;
}
getResult() {
this.result.status = this.status();
return this.result;
}
status(enabled) {
if (this.disabled || enabled === false) {
return 'disabled';
}
if (this.markedTodo) {
return 'todo';
}
if (this.markedPending) {
return 'pending';
}
if (this.result.failedExpectations.length > 0) {
return 'failed';
} else {
return 'passed';
}
}
isExecutable() {
return !this.disabled;
}
getFullName() {
return this.getSpecName(this);
}
isAssertionError(error) {
return (
error instanceof _assert.AssertionError ||
(error && error.name === _assert.AssertionError.name)
);
}
}
exports.default = Spec;
_defineProperty(Spec, 'pendingSpecExceptionMessage', void 0);
Spec.pendingSpecExceptionMessage = '=> marked Pending';
const extractCustomPendingMessage = function (e) {
const fullMessage = e.toString();
const boilerplateStart = fullMessage.indexOf(
Spec.pendingSpecExceptionMessage
);
const boilerplateEnd =
boilerplateStart + Spec.pendingSpecExceptionMessage.length;
return fullMessage.substr(boilerplateEnd);
};