1 | 'use strict';
|
2 |
|
3 | var EventEmitter = require('events').EventEmitter;
|
4 | var Pending = require('./pending');
|
5 | var debug = require('debug')('mocha:runnable');
|
6 | var milliseconds = require('ms');
|
7 | var utils = require('./utils');
|
8 | var errors = require('./errors');
|
9 | var createInvalidExceptionError = errors.createInvalidExceptionError;
|
10 | var createMultipleDoneError = errors.createMultipleDoneError;
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | var Date = global.Date;
|
16 | var setTimeout = global.setTimeout;
|
17 | var clearTimeout = global.clearTimeout;
|
18 | var toString = Object.prototype.toString;
|
19 |
|
20 | module.exports = Runnable;
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | function Runnable(title, fn) {
|
32 | this.title = title;
|
33 | this.fn = fn;
|
34 | this.body = (fn || '').toString();
|
35 | this.async = fn && fn.length;
|
36 | this.sync = !this.async;
|
37 | this._timeout = 2000;
|
38 | this._slow = 75;
|
39 | this._enableTimeouts = true;
|
40 | this._retries = -1;
|
41 | this.reset();
|
42 | }
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | utils.inherits(Runnable, EventEmitter);
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | Runnable.prototype.reset = function() {
|
53 | this.timedOut = false;
|
54 | this._currentRetry = 0;
|
55 | this.pending = false;
|
56 | delete this.state;
|
57 | delete this.err;
|
58 | };
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | Runnable.prototype.timeout = function(ms) {
|
82 | if (!arguments.length) {
|
83 | return this._timeout;
|
84 | }
|
85 | if (typeof ms === 'string') {
|
86 | ms = milliseconds(ms);
|
87 | }
|
88 |
|
89 |
|
90 | var INT_MAX = Math.pow(2, 31) - 1;
|
91 | var range = [0, INT_MAX];
|
92 | ms = utils.clamp(ms, range);
|
93 |
|
94 |
|
95 | if (ms === range[0] || ms === range[1]) {
|
96 | this._enableTimeouts = false;
|
97 | }
|
98 | debug('timeout %d', ms);
|
99 | this._timeout = ms;
|
100 | if (this.timer) {
|
101 | this.resetTimeout();
|
102 | }
|
103 | return this;
|
104 | };
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | Runnable.prototype.slow = function(ms) {
|
114 | if (!arguments.length || typeof ms === 'undefined') {
|
115 | return this._slow;
|
116 | }
|
117 | if (typeof ms === 'string') {
|
118 | ms = milliseconds(ms);
|
119 | }
|
120 | debug('slow %d', ms);
|
121 | this._slow = ms;
|
122 | return this;
|
123 | };
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | Runnable.prototype.enableTimeouts = function(enabled) {
|
133 | if (!arguments.length) {
|
134 | return this._enableTimeouts;
|
135 | }
|
136 | debug('enableTimeouts %s', enabled);
|
137 | this._enableTimeouts = enabled;
|
138 | return this;
|
139 | };
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | Runnable.prototype.skip = function() {
|
148 | this.pending = true;
|
149 | throw new Pending('sync skip; aborting execution');
|
150 | };
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | Runnable.prototype.isPending = function() {
|
158 | return this.pending || (this.parent && this.parent.isPending());
|
159 | };
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 | Runnable.prototype.isFailed = function() {
|
167 | return !this.isPending() && this.state === constants.STATE_FAILED;
|
168 | };
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | Runnable.prototype.isPassed = function() {
|
176 | return !this.isPending() && this.state === constants.STATE_PASSED;
|
177 | };
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 | Runnable.prototype.retries = function(n) {
|
185 | if (!arguments.length) {
|
186 | return this._retries;
|
187 | }
|
188 | this._retries = n;
|
189 | };
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 | Runnable.prototype.currentRetry = function(n) {
|
197 | if (!arguments.length) {
|
198 | return this._currentRetry;
|
199 | }
|
200 | this._currentRetry = n;
|
201 | };
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 | Runnable.prototype.fullTitle = function() {
|
212 | return this.titlePath().join(' ');
|
213 | };
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 | Runnable.prototype.titlePath = function() {
|
223 | return this.parent.titlePath().concat([this.title]);
|
224 | };
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 | Runnable.prototype.clearTimeout = function() {
|
232 | clearTimeout(this.timer);
|
233 | };
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 | Runnable.prototype.resetTimeout = function() {
|
241 | var self = this;
|
242 | var ms = this.timeout() || 1e9;
|
243 |
|
244 | if (!this._enableTimeouts) {
|
245 | return;
|
246 | }
|
247 | this.clearTimeout();
|
248 | this.timer = setTimeout(function() {
|
249 | if (!self._enableTimeouts) {
|
250 | return;
|
251 | }
|
252 | self.callback(self._timeoutError(ms));
|
253 | self.timedOut = true;
|
254 | }, ms);
|
255 | };
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 | Runnable.prototype.globals = function(globals) {
|
264 | if (!arguments.length) {
|
265 | return this._allowedGlobals;
|
266 | }
|
267 | this._allowedGlobals = globals;
|
268 | };
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 | Runnable.prototype.run = function(fn) {
|
277 | var self = this;
|
278 | var start = new Date();
|
279 | var ctx = this.ctx;
|
280 | var finished;
|
281 | var errorWasHandled = false;
|
282 |
|
283 |
|
284 | if (ctx && ctx.runnable) {
|
285 | ctx.runnable(this);
|
286 | }
|
287 |
|
288 |
|
289 | function multiple(err) {
|
290 | if (errorWasHandled) {
|
291 | return;
|
292 | }
|
293 | errorWasHandled = true;
|
294 | self.emit('error', createMultipleDoneError(self, err));
|
295 | }
|
296 |
|
297 |
|
298 | function done(err) {
|
299 | var ms = self.timeout();
|
300 | if (self.timedOut) {
|
301 | return;
|
302 | }
|
303 |
|
304 | if (finished) {
|
305 | return multiple(err);
|
306 | }
|
307 |
|
308 | self.clearTimeout();
|
309 | self.duration = new Date() - start;
|
310 | finished = true;
|
311 | if (!err && self.duration > ms && self._enableTimeouts) {
|
312 | err = self._timeoutError(ms);
|
313 | }
|
314 | fn(err);
|
315 | }
|
316 |
|
317 |
|
318 | this.callback = done;
|
319 |
|
320 | if (this.fn && typeof this.fn.call !== 'function') {
|
321 | done(
|
322 | new TypeError(
|
323 | 'A runnable must be passed a function as its second argument.'
|
324 | )
|
325 | );
|
326 | return;
|
327 | }
|
328 |
|
329 |
|
330 | if (this.async) {
|
331 | this.resetTimeout();
|
332 |
|
333 |
|
334 | this.skip = function asyncSkip() {
|
335 | this.pending = true;
|
336 | done();
|
337 |
|
338 | throw new Pending('async skip; aborting execution');
|
339 | };
|
340 |
|
341 | try {
|
342 | callFnAsync(this.fn);
|
343 | } catch (err) {
|
344 |
|
345 | errorWasHandled = true;
|
346 | if (err instanceof Pending) {
|
347 | return;
|
348 | } else if (this.allowUncaught) {
|
349 | throw err;
|
350 | }
|
351 | done(Runnable.toValueOrError(err));
|
352 | }
|
353 | return;
|
354 | }
|
355 |
|
356 |
|
357 | try {
|
358 | if (this.isPending()) {
|
359 | done();
|
360 | } else {
|
361 | callFn(this.fn);
|
362 | }
|
363 | } catch (err) {
|
364 | errorWasHandled = true;
|
365 | if (err instanceof Pending) {
|
366 | return done();
|
367 | } else if (this.allowUncaught) {
|
368 | throw err;
|
369 | }
|
370 | done(Runnable.toValueOrError(err));
|
371 | }
|
372 |
|
373 | function callFn(fn) {
|
374 | var result = fn.call(ctx);
|
375 | if (result && typeof result.then === 'function') {
|
376 | self.resetTimeout();
|
377 | result.then(
|
378 | function() {
|
379 | done();
|
380 |
|
381 |
|
382 | return null;
|
383 | },
|
384 | function(reason) {
|
385 | done(reason || new Error('Promise rejected with no or falsy reason'));
|
386 | }
|
387 | );
|
388 | } else {
|
389 | if (self.asyncOnly) {
|
390 | return done(
|
391 | new Error(
|
392 | '--async-only option in use without declaring `done()` or returning a promise'
|
393 | )
|
394 | );
|
395 | }
|
396 |
|
397 | done();
|
398 | }
|
399 | }
|
400 |
|
401 | function callFnAsync(fn) {
|
402 | var result = fn.call(ctx, function(err) {
|
403 | if (err instanceof Error || toString.call(err) === '[object Error]') {
|
404 | return done(err);
|
405 | }
|
406 | if (err) {
|
407 | if (Object.prototype.toString.call(err) === '[object Object]') {
|
408 | return done(
|
409 | new Error('done() invoked with non-Error: ' + JSON.stringify(err))
|
410 | );
|
411 | }
|
412 | return done(new Error('done() invoked with non-Error: ' + err));
|
413 | }
|
414 | if (result && utils.isPromise(result)) {
|
415 | return done(
|
416 | new Error(
|
417 | 'Resolution method is overspecified. Specify a callback *or* return a Promise; not both.'
|
418 | )
|
419 | );
|
420 | }
|
421 |
|
422 | done();
|
423 | });
|
424 | }
|
425 | };
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 |
|
434 | Runnable.prototype._timeoutError = function(ms) {
|
435 | var msg =
|
436 | 'Timeout of ' +
|
437 | ms +
|
438 | 'ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.';
|
439 | if (this.file) {
|
440 | msg += ' (' + this.file + ')';
|
441 | }
|
442 | return new Error(msg);
|
443 | };
|
444 |
|
445 | var constants = utils.defineConstants(
|
446 | |
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 |
|
453 |
|
454 |
|
455 | {
|
456 | |
457 |
|
458 |
|
459 | STATE_FAILED: 'failed',
|
460 | |
461 |
|
462 |
|
463 | STATE_PASSED: 'passed'
|
464 | }
|
465 | );
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 | Runnable.toValueOrError = function(value) {
|
474 | return (
|
475 | value ||
|
476 | createInvalidExceptionError(
|
477 | 'Runnable failed with falsy or undefined exception. Please throw an Error instead.',
|
478 | value
|
479 | )
|
480 | );
|
481 | };
|
482 |
|
483 | Runnable.constants = constants;
|