mocha 7.1.1 → 8.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/runnable.js CHANGED
@@ -5,8 +5,9 @@ var Pending = require('./pending');
5
5
  var debug = require('debug')('mocha:runnable');
6
6
  var milliseconds = require('ms');
7
7
  var utils = require('./utils');
8
- var createInvalidExceptionError = require('./errors')
9
- .createInvalidExceptionError;
8
+ var errors = require('./errors');
9
+ var createInvalidExceptionError = errors.createInvalidExceptionError;
10
+ var createMultipleDoneError = errors.createMultipleDoneError;
10
11
 
11
12
  /**
12
13
  * Save timer references to avoid Sinon interfering (see GH-237).
@@ -35,11 +36,8 @@ function Runnable(title, fn) {
35
36
  this.sync = !this.async;
36
37
  this._timeout = 2000;
37
38
  this._slow = 75;
38
- this._enableTimeouts = true;
39
- this.timedOut = false;
40
39
  this._retries = -1;
41
- this._currentRetry = 0;
42
- this.pending = false;
40
+ this.reset();
43
41
  }
44
42
 
45
43
  /**
@@ -47,6 +45,17 @@ function Runnable(title, fn) {
47
45
  */
48
46
  utils.inherits(Runnable, EventEmitter);
49
47
 
48
+ /**
49
+ * Resets the state initially or for a next run.
50
+ */
51
+ Runnable.prototype.reset = function() {
52
+ this.timedOut = false;
53
+ this._currentRetry = 0;
54
+ this.pending = false;
55
+ delete this.state;
56
+ delete this.err;
57
+ };
58
+
50
59
  /**
51
60
  * Get current timeout value in msecs.
52
61
  *
@@ -83,10 +92,12 @@ Runnable.prototype.timeout = function(ms) {
83
92
 
84
93
  // see #1652 for reasoning
85
94
  if (ms === range[0] || ms === range[1]) {
86
- this._enableTimeouts = false;
95
+ this._timeout = 0;
96
+ } else {
97
+ this._timeout = ms;
87
98
  }
88
- debug('timeout %d', ms);
89
- this._timeout = ms;
99
+ debug('timeout %d', this._timeout);
100
+
90
101
  if (this.timer) {
91
102
  this.resetTimeout();
92
103
  }
@@ -112,22 +123,6 @@ Runnable.prototype.slow = function(ms) {
112
123
  return this;
113
124
  };
114
125
 
115
- /**
116
- * Set and get whether timeout is `enabled`.
117
- *
118
- * @private
119
- * @param {boolean} enabled
120
- * @return {Runnable|boolean} enabled or Runnable instance.
121
- */
122
- Runnable.prototype.enableTimeouts = function(enabled) {
123
- if (!arguments.length) {
124
- return this._enableTimeouts;
125
- }
126
- debug('enableTimeouts %s', enabled);
127
- this._enableTimeouts = enabled;
128
- return this;
129
- };
130
-
131
126
  /**
132
127
  * Halt and mark as pending.
133
128
  *
@@ -222,31 +217,6 @@ Runnable.prototype.clearTimeout = function() {
222
217
  clearTimeout(this.timer);
223
218
  };
224
219
 
225
- /**
226
- * Inspect the runnable void of private properties.
227
- *
228
- * @private
229
- * @return {string}
230
- */
231
- Runnable.prototype.inspect = function() {
232
- return JSON.stringify(
233
- this,
234
- function(key, val) {
235
- if (key[0] === '_') {
236
- return;
237
- }
238
- if (key === 'parent') {
239
- return '#<Suite>';
240
- }
241
- if (key === 'ctx') {
242
- return '#<Context>';
243
- }
244
- return val;
245
- },
246
- 2
247
- );
248
- };
249
-
250
220
  /**
251
221
  * Reset the timeout.
252
222
  *
@@ -254,14 +224,14 @@ Runnable.prototype.inspect = function() {
254
224
  */
255
225
  Runnable.prototype.resetTimeout = function() {
256
226
  var self = this;
257
- var ms = this.timeout() || 1e9;
227
+ var ms = this.timeout();
258
228
 
259
- if (!this._enableTimeouts) {
229
+ if (ms === 0) {
260
230
  return;
261
231
  }
262
232
  this.clearTimeout();
263
233
  this.timer = setTimeout(function() {
264
- if (!self._enableTimeouts) {
234
+ if (self.timeout() === 0) {
265
235
  return;
266
236
  }
267
237
  self.callback(self._timeoutError(ms));
@@ -293,7 +263,9 @@ Runnable.prototype.run = function(fn) {
293
263
  var start = new Date();
294
264
  var ctx = this.ctx;
295
265
  var finished;
296
- var emitted;
266
+ var errorWasHandled = false;
267
+
268
+ if (this.isPending()) return fn();
297
269
 
298
270
  // Sometimes the ctx exists, but it is not runnable
299
271
  if (ctx && ctx.runnable) {
@@ -302,17 +274,11 @@ Runnable.prototype.run = function(fn) {
302
274
 
303
275
  // called multiple times
304
276
  function multiple(err) {
305
- if (emitted) {
277
+ if (errorWasHandled) {
306
278
  return;
307
279
  }
308
- emitted = true;
309
- var msg = 'done() called multiple times';
310
- if (err && err.message) {
311
- err.message += " (and Mocha's " + msg + ')';
312
- self.emit('error', err);
313
- } else {
314
- self.emit('error', new Error(msg));
315
- }
280
+ errorWasHandled = true;
281
+ self.emit('error', createMultipleDoneError(self, err));
316
282
  }
317
283
 
318
284
  // finished
@@ -329,7 +295,7 @@ Runnable.prototype.run = function(fn) {
329
295
  self.clearTimeout();
330
296
  self.duration = new Date() - start;
331
297
  finished = true;
332
- if (!err && self.duration > ms && self._enableTimeouts) {
298
+ if (!err && self.duration > ms && ms > 0) {
333
299
  err = self._timeoutError(ms);
334
300
  }
335
301
  fn(err);
@@ -363,7 +329,7 @@ Runnable.prototype.run = function(fn) {
363
329
  callFnAsync(this.fn);
364
330
  } catch (err) {
365
331
  // handles async runnables which actually run synchronously
366
- emitted = true;
332
+ errorWasHandled = true;
367
333
  if (err instanceof Pending) {
368
334
  return; // done() is already called in this.skip()
369
335
  } else if (this.allowUncaught) {
@@ -376,13 +342,9 @@ Runnable.prototype.run = function(fn) {
376
342
 
377
343
  // sync or promise-returning
378
344
  try {
379
- if (this.isPending()) {
380
- done();
381
- } else {
382
- callFn(this.fn);
383
- }
345
+ callFn(this.fn);
384
346
  } catch (err) {
385
- emitted = true;
347
+ errorWasHandled = true;
386
348
  if (err instanceof Pending) {
387
349
  return done();
388
350
  } else if (this.allowUncaught) {
@@ -481,7 +443,11 @@ var constants = utils.defineConstants(
481
443
  /**
482
444
  * Value of `state` prop when a `Runnable` has passed
483
445
  */
484
- STATE_PASSED: 'passed'
446
+ STATE_PASSED: 'passed',
447
+ /**
448
+ * Value of `state` prop when a `Runnable` has been skipped by user
449
+ */
450
+ STATE_PENDING: 'pending'
485
451
  }
486
452
  );
487
453