mocha 6.0.0-1 → 6.0.0

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.
@@ -7,7 +7,13 @@
7
7
  */
8
8
 
9
9
  var Base = require('./base');
10
+ var constants = require('../runner').constants;
10
11
  var inherits = require('../utils').inherits;
12
+ var EVENT_RUN_BEGIN = constants.EVENT_RUN_BEGIN;
13
+ var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING;
14
+ var EVENT_TEST_PASS = constants.EVENT_TEST_PASS;
15
+ var EVENT_RUN_END = constants.EVENT_RUN_END;
16
+ var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL;
11
17
 
12
18
  /**
13
19
  * Expose `Dot`.
@@ -40,24 +46,24 @@ function NyanCat(runner) {
40
46
  this.trajectories = [[], [], [], []];
41
47
  this.trajectoryWidthMax = width - nyanCatWidth;
42
48
 
43
- runner.on('start', function() {
49
+ runner.on(EVENT_RUN_BEGIN, function() {
44
50
  Base.cursor.hide();
45
51
  self.draw();
46
52
  });
47
53
 
48
- runner.on('pending', function() {
54
+ runner.on(EVENT_TEST_PENDING, function() {
49
55
  self.draw();
50
56
  });
51
57
 
52
- runner.on('pass', function() {
58
+ runner.on(EVENT_TEST_PASS, function() {
53
59
  self.draw();
54
60
  });
55
61
 
56
- runner.on('fail', function() {
62
+ runner.on(EVENT_TEST_FAIL, function() {
57
63
  self.draw();
58
64
  });
59
65
 
60
- runner.once('end', function() {
66
+ runner.once(EVENT_RUN_END, function() {
61
67
  Base.cursor.show();
62
68
  for (var i = 0; i < self.numberOfLines; i++) {
63
69
  write('\n');
@@ -7,6 +7,10 @@
7
7
  */
8
8
 
9
9
  var Base = require('./base');
10
+ var constants = require('../runner').constants;
11
+ var EVENT_RUN_BEGIN = constants.EVENT_RUN_BEGIN;
12
+ var EVENT_TEST_END = constants.EVENT_TEST_END;
13
+ var EVENT_RUN_END = constants.EVENT_RUN_END;
10
14
  var inherits = require('../utils').inherits;
11
15
  var color = Base.color;
12
16
  var cursor = Base.cursor;
@@ -53,13 +57,13 @@ function Progress(runner, options) {
53
57
  options.verbose = reporterOptions.verbose || false;
54
58
 
55
59
  // tests started
56
- runner.on('start', function() {
60
+ runner.on(EVENT_RUN_BEGIN, function() {
57
61
  console.log();
58
62
  cursor.hide();
59
63
  });
60
64
 
61
65
  // tests complete
62
- runner.on('test end', function() {
66
+ runner.on(EVENT_TEST_END, function() {
63
67
  complete++;
64
68
 
65
69
  var percent = complete / total;
@@ -85,7 +89,7 @@ function Progress(runner, options) {
85
89
 
86
90
  // tests are complete, output some stats
87
91
  // and the failures if any
88
- runner.once('end', function() {
92
+ runner.once(EVENT_RUN_END, function() {
89
93
  cursor.show();
90
94
  console.log();
91
95
  self.epilogue();
@@ -7,6 +7,14 @@
7
7
  */
8
8
 
9
9
  var Base = require('./base');
10
+ var constants = require('../runner').constants;
11
+ var EVENT_RUN_BEGIN = constants.EVENT_RUN_BEGIN;
12
+ var EVENT_RUN_END = constants.EVENT_RUN_END;
13
+ var EVENT_SUITE_BEGIN = constants.EVENT_SUITE_BEGIN;
14
+ var EVENT_SUITE_END = constants.EVENT_SUITE_END;
15
+ var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL;
16
+ var EVENT_TEST_PASS = constants.EVENT_TEST_PASS;
17
+ var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING;
10
18
  var inherits = require('../utils').inherits;
11
19
  var color = Base.color;
12
20
 
@@ -36,28 +44,28 @@ function Spec(runner) {
36
44
  return Array(indents).join(' ');
37
45
  }
38
46
 
39
- runner.on('start', function() {
47
+ runner.on(EVENT_RUN_BEGIN, function() {
40
48
  console.log();
41
49
  });
42
50
 
43
- runner.on('suite', function(suite) {
51
+ runner.on(EVENT_SUITE_BEGIN, function(suite) {
44
52
  ++indents;
45
53
  console.log(color('suite', '%s%s'), indent(), suite.title);
46
54
  });
47
55
 
48
- runner.on('suite end', function() {
56
+ runner.on(EVENT_SUITE_END, function() {
49
57
  --indents;
50
58
  if (indents === 1) {
51
59
  console.log();
52
60
  }
53
61
  });
54
62
 
55
- runner.on('pending', function(test) {
63
+ runner.on(EVENT_TEST_PENDING, function(test) {
56
64
  var fmt = indent() + color('pending', ' - %s');
57
65
  console.log(fmt, test.title);
58
66
  });
59
67
 
60
- runner.on('pass', function(test) {
68
+ runner.on(EVENT_TEST_PASS, function(test) {
61
69
  var fmt;
62
70
  if (test.speed === 'fast') {
63
71
  fmt =
@@ -75,11 +83,11 @@ function Spec(runner) {
75
83
  }
76
84
  });
77
85
 
78
- runner.on('fail', function(test) {
86
+ runner.on(EVENT_TEST_FAIL, function(test) {
79
87
  console.log(indent() + color('fail', ' %d) %s'), ++n, test.title);
80
88
  });
81
89
 
82
- runner.once('end', self.epilogue.bind(self));
90
+ runner.once(EVENT_RUN_END, self.epilogue.bind(self));
83
91
  }
84
92
 
85
93
  /**
@@ -8,6 +8,13 @@
8
8
 
9
9
  var util = require('util');
10
10
  var Base = require('./base');
11
+ var constants = require('../runner').constants;
12
+ var EVENT_TEST_PASS = constants.EVENT_TEST_PASS;
13
+ var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL;
14
+ var EVENT_RUN_BEGIN = constants.EVENT_RUN_BEGIN;
15
+ var EVENT_RUN_END = constants.EVENT_RUN_END;
16
+ var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING;
17
+ var EVENT_TEST_END = constants.EVENT_TEST_END;
11
18
  var inherits = require('../utils').inherits;
12
19
  var sprintf = util.format;
13
20
 
@@ -42,29 +49,29 @@ function TAP(runner, options) {
42
49
 
43
50
  this._producer = createProducer(tapVersion);
44
51
 
45
- runner.once('start', function() {
52
+ runner.once(EVENT_RUN_BEGIN, function() {
46
53
  var ntests = runner.grepTotal(runner.suite);
47
54
  self._producer.writeVersion();
48
55
  self._producer.writePlan(ntests);
49
56
  });
50
57
 
51
- runner.on('test end', function() {
58
+ runner.on(EVENT_TEST_END, function() {
52
59
  ++n;
53
60
  });
54
61
 
55
- runner.on('pending', function(test) {
62
+ runner.on(EVENT_TEST_PENDING, function(test) {
56
63
  self._producer.writePending(n, test);
57
64
  });
58
65
 
59
- runner.on('pass', function(test) {
66
+ runner.on(EVENT_TEST_PASS, function(test) {
60
67
  self._producer.writePass(n, test);
61
68
  });
62
69
 
63
- runner.on('fail', function(test, err) {
70
+ runner.on(EVENT_TEST_FAIL, function(test, err) {
64
71
  self._producer.writeFail(n, test, err);
65
72
  });
66
73
 
67
- runner.once('end', function() {
74
+ runner.once(EVENT_RUN_END, function() {
68
75
  self._producer.writeEpilogue(runner.stats);
69
76
  });
70
77
  }
@@ -8,13 +8,20 @@
8
8
 
9
9
  var Base = require('./base');
10
10
  var utils = require('../utils');
11
- var inherits = utils.inherits;
12
11
  var fs = require('fs');
13
- var escape = utils.escape;
14
12
  var mkdirp = require('mkdirp');
15
13
  var path = require('path');
16
14
  var errors = require('../errors');
17
- var createNotSupportedError = errors.createNotSupportedError;
15
+ var createUnsupportedError = errors.createUnsupportedError;
16
+ var constants = require('../runner').constants;
17
+ var EVENT_TEST_PASS = constants.EVENT_TEST_PASS;
18
+ var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL;
19
+ var EVENT_RUN_END = constants.EVENT_RUN_END;
20
+ var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING;
21
+ var STATE_FAILED = require('../runnable').constants.STATE_FAILED;
22
+ var inherits = utils.inherits;
23
+ var escape = utils.escape;
24
+
18
25
  /**
19
26
  * Save timer references to avoid Sinon interfering (see GH-237).
20
27
  */
@@ -51,7 +58,7 @@ function XUnit(runner, options) {
51
58
  if (options && options.reporterOptions) {
52
59
  if (options.reporterOptions.output) {
53
60
  if (!fs.createWriteStream) {
54
- throw createNotSupportedError('file output not supported in browser');
61
+ throw createUnsupportedError('file output not supported in browser');
55
62
  }
56
63
 
57
64
  mkdirp.sync(path.dirname(options.reporterOptions.output));
@@ -65,19 +72,19 @@ function XUnit(runner, options) {
65
72
  // fall back to the default suite name
66
73
  suiteName = suiteName || DEFAULT_SUITE_NAME;
67
74
 
68
- runner.on('pending', function(test) {
75
+ runner.on(EVENT_TEST_PENDING, function(test) {
69
76
  tests.push(test);
70
77
  });
71
78
 
72
- runner.on('pass', function(test) {
79
+ runner.on(EVENT_TEST_PASS, function(test) {
73
80
  tests.push(test);
74
81
  });
75
82
 
76
- runner.on('fail', function(test) {
83
+ runner.on(EVENT_TEST_FAIL, function(test) {
77
84
  tests.push(test);
78
85
  });
79
86
 
80
- runner.once('end', function() {
87
+ runner.once(EVENT_RUN_END, function() {
81
88
  self.write(
82
89
  tag(
83
90
  'testsuite',
@@ -152,7 +159,7 @@ XUnit.prototype.test = function(test) {
152
159
  time: test.duration / 1000 || 0
153
160
  };
154
161
 
155
- if (test.state === 'failed') {
162
+ if (test.state === STATE_FAILED) {
156
163
  var err = test.err;
157
164
  var diff =
158
165
  Base.hideDiff || !err.actual || !err.expected
package/lib/runnable.js CHANGED
@@ -1,9 +1,12 @@
1
1
  'use strict';
2
+
2
3
  var EventEmitter = require('events').EventEmitter;
3
4
  var Pending = require('./pending');
4
5
  var debug = require('debug')('mocha:runnable');
5
6
  var milliseconds = require('ms');
6
7
  var utils = require('./utils');
8
+ var createInvalidExceptionError = require('./errors')
9
+ .createInvalidExceptionError;
7
10
 
8
11
  /**
9
12
  * Save timer references to avoid Sinon interfering (see GH-237).
@@ -16,10 +19,11 @@ var toString = Object.prototype.toString;
16
19
  module.exports = Runnable;
17
20
 
18
21
  /**
19
- * Initialize a new `Runnable` with the given `title` and callback `fn`. Derived from [EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter)
22
+ * Initialize a new `Runnable` with the given `title` and callback `fn`.
20
23
  *
21
24
  * @class
22
- * @extends EventEmitter
25
+ * @extends external:EventEmitter
26
+ * @public
23
27
  * @param {String} title
24
28
  * @param {Function} fn
25
29
  */
@@ -149,7 +153,7 @@ Runnable.prototype.isPending = function() {
149
153
  * @private
150
154
  */
151
155
  Runnable.prototype.isFailed = function() {
152
- return !this.isPending() && this.state === 'failed';
156
+ return !this.isPending() && this.state === constants.STATE_FAILED;
153
157
  };
154
158
 
155
159
  /**
@@ -158,7 +162,7 @@ Runnable.prototype.isFailed = function() {
158
162
  * @private
159
163
  */
160
164
  Runnable.prototype.isPassed = function() {
161
- return !this.isPending() && this.state === 'passed';
165
+ return !this.isPending() && this.state === constants.STATE_PASSED;
162
166
  };
163
167
 
164
168
  /**
@@ -353,7 +357,7 @@ Runnable.prototype.run = function(fn) {
353
357
  callFnAsync(this.fn);
354
358
  } catch (err) {
355
359
  emitted = true;
356
- done(utils.getError(err));
360
+ done(Runnable.toValueOrError(err));
357
361
  }
358
362
  return;
359
363
  }
@@ -376,7 +380,7 @@ Runnable.prototype.run = function(fn) {
376
380
  }
377
381
  } catch (err) {
378
382
  emitted = true;
379
- done(utils.getError(err));
383
+ done(Runnable.toValueOrError(err));
380
384
  }
381
385
 
382
386
  function callFn(fn) {
@@ -450,3 +454,43 @@ Runnable.prototype._timeoutError = function(ms) {
450
454
  }
451
455
  return new Error(msg);
452
456
  };
457
+
458
+ var constants = utils.defineConstants(
459
+ /**
460
+ * {@link Runnable}-related constants.
461
+ * @public
462
+ * @memberof Runnable
463
+ * @readonly
464
+ * @static
465
+ * @alias constants
466
+ * @enum {string}
467
+ */
468
+ {
469
+ /**
470
+ * Value of `state` prop when a `Runnable` has failed
471
+ */
472
+ STATE_FAILED: 'failed',
473
+ /**
474
+ * Value of `state` prop when a `Runnable` has passed
475
+ */
476
+ STATE_PASSED: 'passed'
477
+ }
478
+ );
479
+
480
+ /**
481
+ * Given `value`, return identity if truthy, otherwise create an "invalid exception" error and return that.
482
+ * @param {*} [value] - Value to return, if present
483
+ * @returns {*|Error} `value`, otherwise an `Error`
484
+ * @private
485
+ */
486
+ Runnable.toValueOrError = function(value) {
487
+ return (
488
+ value ||
489
+ createInvalidExceptionError(
490
+ 'Runnable failed with falsy or undefined exception. Please throw an Error instead.',
491
+ value
492
+ )
493
+ );
494
+ };
495
+
496
+ Runnable.constants = constants;