mocha 5.0.3 → 5.1.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.
@@ -0,0 +1,128 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Module dependencies.
5
+ */
6
+
7
+ var Base = require('./base');
8
+
9
+ /**
10
+ * Expose `JSON`.
11
+ */
12
+
13
+ exports = module.exports = JSONReporter;
14
+
15
+ /**
16
+ * Initialize a new `JSON` reporter.
17
+ *
18
+ * @api public
19
+ * @param {Runner} runner
20
+ * @param {options} mocha invocation options. Invoking
21
+ * `mocha -R --reporter-options output-file=asdf` yields options like:
22
+ * { ... reporterOptions: { "output-file": "asdf" } ... }
23
+ */
24
+ function JSONReporter (runner, options) {
25
+ <<<<<<< HEAD
26
+ =======
27
+ options = options || {};
28
+ var reptOptions = options.reporterOptions || {};
29
+ >>>>>>> + json ouput controls: output-file, output-object
30
+ Base.call(this, runner);
31
+
32
+ var self = this;
33
+ var tests = [];
34
+ var pending = [];
35
+ var failures = [];
36
+ var passes = [];
37
+
38
+ runner.on('test end', function (test) {
39
+ tests.push(test);
40
+ });
41
+
42
+ runner.on('pass', function (test) {
43
+ passes.push(test);
44
+ });
45
+
46
+ runner.on('fail', function (test) {
47
+ failures.push(test);
48
+ });
49
+
50
+ runner.on('pending', function (test) {
51
+ pending.push(test);
52
+ });
53
+
54
+ runner.once('end', function () {
55
+ var obj = {
56
+ stats: self.stats,
57
+ tests: tests.map(clean),
58
+ pending: pending.map(clean),
59
+ failures: failures.map(clean),
60
+ passes: passes.map(clean)
61
+ };
62
+
63
+ runner.testResults = obj;
64
+ <<<<<<< HEAD
65
+ if ('output-object' in options.reporterOptions) {
66
+ // Pass to reporter with: reporter("json", {"output-object": myObject})
67
+ Object.assign(options.reporterOptions['output-object'], obj);
68
+ } else {
69
+ var text = JSON.stringify(obj, null, 2);
70
+ if ('output-file' in options.reporterOptions) {
71
+ // Direct output with `mocha -R --reporter-options output-file=rpt.json`
72
+ try {
73
+ require('fs').writeFileSync(options.reporterOptions['output-file'], text);
74
+ } catch (e) {
75
+ console.warn('error writing to ' + options.reporterOptions.output + ':', e);
76
+ =======
77
+ if ('output-object' in reptOptions) {
78
+ // Pass to reporter with: reporter("json", {"output-object": myObject})
79
+ Object.assign(reptOptions['output-object'], obj);
80
+ } else {
81
+ var text = JSON.stringify(obj, null, 2);
82
+ if ('output-file' in reptOptions) {
83
+ // Direct output with `mocha -R --reporter-options output-file=rpt.json`
84
+ try {
85
+ require('fs').writeFileSync(reptOptions['output-file'], text);
86
+ } catch (e) {
87
+ console.warn('error writing to ' + reptOptions.output + ':', e);
88
+ >>>>>>> + json ouput controls: output-file, output-object
89
+ }
90
+ } else {
91
+ process.stdout.write(text);
92
+ }
93
+ }
94
+ });
95
+ }
96
+
97
+ /**
98
+ * Return a plain-object representation of `test`
99
+ * free of cyclic properties etc.
100
+ *
101
+ * @api private
102
+ * @param {Object} test
103
+ * @return {Object}
104
+ */
105
+ function clean (test) {
106
+ return {
107
+ title: test.title,
108
+ fullTitle: test.fullTitle(),
109
+ duration: test.duration,
110
+ currentRetry: test.currentRetry(),
111
+ err: errorJSON(test.err || {})
112
+ };
113
+ }
114
+
115
+ /**
116
+ * Transform `error` into a JSON object.
117
+ *
118
+ * @api private
119
+ * @param {Error} err
120
+ * @return {Object}
121
+ */
122
+ function errorJSON (err) {
123
+ var res = {};
124
+ Object.getOwnPropertyNames(err).forEach(function (key) {
125
+ res[key] = err[key];
126
+ }, err);
127
+ return res;
128
+ }
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
-
2
+ /**
3
+ * @module Landing
4
+ */
3
5
  /**
4
6
  * Module dependencies.
5
7
  */
@@ -36,6 +38,10 @@ Base.colors.runway = 90;
36
38
  /**
37
39
  * Initialize a new `Landing` reporter.
38
40
  *
41
+ * @public
42
+ * @class
43
+ * @memberof Mocha.reporters
44
+ * @extends Mocha.reporters.Base
39
45
  * @api public
40
46
  * @param {Runner} runner
41
47
  */
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
-
2
+ /**
3
+ * @module List
4
+ */
3
5
  /**
4
6
  * Module dependencies.
5
7
  */
@@ -18,6 +20,10 @@ exports = module.exports = List;
18
20
  /**
19
21
  * Initialize a new `List` test reporter.
20
22
  *
23
+ * @public
24
+ * @class
25
+ * @memberof Mocha.reporters
26
+ * @extends Mocha.reporters.Base
21
27
  * @api public
22
28
  * @param {Runner} runner
23
29
  */
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
-
2
+ /**
3
+ * @module Markdown
4
+ */
3
5
  /**
4
6
  * Module dependencies.
5
7
  */
@@ -22,6 +24,10 @@ exports = module.exports = Markdown;
22
24
  /**
23
25
  * Initialize a new `Markdown` reporter.
24
26
  *
27
+ * @public
28
+ * @class
29
+ * @memberof Mocha.reporters
30
+ * @extends Mocha.reporters.Base
25
31
  * @api public
26
32
  * @param {Runner} runner
27
33
  */
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
-
2
+ /**
3
+ * @module Min
4
+ */
3
5
  /**
4
6
  * Module dependencies.
5
7
  */
@@ -16,6 +18,10 @@ exports = module.exports = Min;
16
18
  /**
17
19
  * Initialize a new `Min` minimal test reporter (best used with --watch).
18
20
  *
21
+ * @public
22
+ * @class
23
+ * @memberof Mocha.reporters
24
+ * @extends Mocha.reporters.Base
19
25
  * @api public
20
26
  * @param {Runner} runner
21
27
  */
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
-
2
+ /**
3
+ * @module Nyan
4
+ */
3
5
  /**
4
6
  * Module dependencies.
5
7
  */
@@ -18,6 +20,10 @@ exports = module.exports = NyanCat;
18
20
  *
19
21
  * @param {Runner} runner
20
22
  * @api public
23
+ * @public
24
+ * @class Nyan
25
+ * @memberof Mocha.reporters
26
+ * @extends Mocha.reporters.Base
21
27
  */
22
28
 
23
29
  function NyanCat (runner) {
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
-
2
+ /**
3
+ * @module Progress
4
+ */
3
5
  /**
4
6
  * Module dependencies.
5
7
  */
@@ -24,6 +26,10 @@ Base.colors.progress = 90;
24
26
  /**
25
27
  * Initialize a new `Progress` bar test reporter.
26
28
  *
29
+ * @public
30
+ * @class
31
+ * @memberof Mocha.reporters
32
+ * @extends Mocha.reporters.Base
27
33
  * @api public
28
34
  * @param {Runner} runner
29
35
  * @param {Object} options
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
-
2
+ /**
3
+ * @module Spec
4
+ */
3
5
  /**
4
6
  * Module dependencies.
5
7
  */
@@ -17,6 +19,10 @@ exports = module.exports = Spec;
17
19
  /**
18
20
  * Initialize a new `Spec` test reporter.
19
21
  *
22
+ * @public
23
+ * @class
24
+ * @memberof Mocha.reporters
25
+ * @extends Mocha.reporters.Base
20
26
  * @api public
21
27
  * @param {Runner} runner
22
28
  */
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
-
2
+ /**
3
+ * @module TAP
4
+ */
3
5
  /**
4
6
  * Module dependencies.
5
7
  */
@@ -15,6 +17,10 @@ exports = module.exports = TAP;
15
17
  /**
16
18
  * Initialize a new `TAP` reporter.
17
19
  *
20
+ * @public
21
+ * @class
22
+ * @memberof Mocha.reporters
23
+ * @extends Mocha.reporters.Base
18
24
  * @api public
19
25
  * @param {Runner} runner
20
26
  */
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
-
2
+ /**
3
+ * @module XUnit
4
+ */
3
5
  /**
4
6
  * Module dependencies.
5
7
  */
@@ -33,6 +35,10 @@ exports = module.exports = XUnit;
33
35
  /**
34
36
  * Initialize a new `XUnit` reporter.
35
37
  *
38
+ * @public
39
+ * @class
40
+ * @memberof Mocha.reporters
41
+ * @extends Mocha.reporters.Base
36
42
  * @api public
37
43
  * @param {Runner} runner
38
44
  */
package/lib/runnable.js CHANGED
@@ -1,9 +1,10 @@
1
1
  'use strict';
2
-
2
+ /**
3
+ * @module Runnable
4
+ */
3
5
  /**
4
6
  * Module dependencies.
5
7
  */
6
-
7
8
  var EventEmitter = require('events').EventEmitter;
8
9
  var Pending = require('./pending');
9
10
  var debug = require('debug')('mocha:runnable');
@@ -35,13 +36,13 @@ var toString = Object.prototype.toString;
35
36
  module.exports = Runnable;
36
37
 
37
38
  /**
38
- * Initialize a new `Runnable` with the given `title` and callback `fn`.
39
+ * Initialize a new `Runnable` with the given `title` and callback `fn`. Derived from [EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter)
39
40
  *
41
+ * @memberof Mocha
42
+ * @public
43
+ * @class
40
44
  * @param {String} title
41
45
  * @param {Function} fn
42
- * @api private
43
- * @param {string} title
44
- * @param {Function} fn
45
46
  */
46
47
  function Runnable (title, fn) {
47
48
  this.title = title;
@@ -103,7 +104,7 @@ Runnable.prototype.slow = function (ms) {
103
104
  if (typeof ms === 'string') {
104
105
  ms = milliseconds(ms);
105
106
  }
106
- debug('timeout %d', ms);
107
+ debug('slow %d', ms);
107
108
  this._slow = ms;
108
109
  return this;
109
110
  };
@@ -127,6 +128,8 @@ Runnable.prototype.enableTimeouts = function (enabled) {
127
128
  /**
128
129
  * Halt and mark as pending.
129
130
  *
131
+ * @memberof Mocha.Runnable
132
+ * @public
130
133
  * @api public
131
134
  */
132
135
  Runnable.prototype.skip = function () {
@@ -188,6 +191,8 @@ Runnable.prototype.currentRetry = function (n) {
188
191
  * Return the full title generated by recursively concatenating the parent's
189
192
  * full title.
190
193
  *
194
+ * @memberof Mocha.Runnable
195
+ * @public
191
196
  * @api public
192
197
  * @return {string}
193
198
  */
@@ -198,6 +203,8 @@ Runnable.prototype.fullTitle = function () {
198
203
  /**
199
204
  * Return the title path generated by concatenating the parent's title path with the title.
200
205
  *
206
+ * @memberof Mocha.Runnable
207
+ * @public
201
208
  * @api public
202
209
  * @return {string}
203
210
  */
@@ -252,8 +259,7 @@ Runnable.prototype.resetTimeout = function () {
252
259
  if (!self._enableTimeouts) {
253
260
  return;
254
261
  }
255
- self.callback(new Error('Timeout of ' + ms +
256
- 'ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.'));
262
+ self.callback(self._timeoutError(ms));
257
263
  self.timedOut = true;
258
264
  }, ms);
259
265
  };
@@ -319,8 +325,7 @@ Runnable.prototype.run = function (fn) {
319
325
  self.duration = new Date() - start;
320
326
  finished = true;
321
327
  if (!err && self.duration > ms && self._enableTimeouts) {
322
- err = new Error('Timeout of ' + ms +
323
- 'ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.');
328
+ err = self._timeoutError(ms);
324
329
  }
325
330
  fn(err);
326
331
  }
@@ -417,3 +422,18 @@ Runnable.prototype.run = function (fn) {
417
422
  });
418
423
  }
419
424
  };
425
+
426
+ /**
427
+ * Instantiates a "timeout" error
428
+ *
429
+ * @param {number} ms - Timeout (in milliseconds)
430
+ * @returns {Error} a "timeout" error
431
+ * @private
432
+ */
433
+ Runnable.prototype._timeoutError = function (ms) {
434
+ var msg = 'Timeout of ' + ms + 'ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.';
435
+ if (this.file) {
436
+ msg += ' (' + this.file + ')';
437
+ }
438
+ return new Error(msg);
439
+ };
package/lib/runner.js CHANGED
@@ -1,9 +1,11 @@
1
1
  'use strict';
2
2
 
3
+ /**
4
+ * @module Runner
5
+ */
3
6
  /**
4
7
  * Module dependencies.
5
8
  */
6
-
7
9
  var EventEmitter = require('events').EventEmitter;
8
10
  var Pending = require('./pending');
9
11
  var utils = require('./utils');
@@ -37,7 +39,7 @@ var globals = [
37
39
  module.exports = Runner;
38
40
 
39
41
  /**
40
- * Initialize a `Runner` for the given `suite`.
42
+ * Initialize a `Runner` for the given `suite`. Derived from [EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter)
41
43
  *
42
44
  * Events:
43
45
  *
@@ -53,8 +55,11 @@ module.exports = Runner;
53
55
  * - `fail` (test, err) test failed
54
56
  * - `pending` (test) test pending
55
57
  *
58
+ * @memberof Mocha
59
+ * @public
60
+ * @class
56
61
  * @api public
57
- * @param {Suite} suite Root suite
62
+ * @param {Suite} [suite] Root suite
58
63
  * @param {boolean} [delay] Whether or not to delay execution of root suite
59
64
  * until ready.
60
65
  */
@@ -95,10 +100,9 @@ inherits(Runner, EventEmitter);
95
100
  * Run tests with full titles matching `re`. Updates runner.total
96
101
  * with number of tests matched.
97
102
  *
98
- * @param {RegExp} re
99
- * @param {Boolean} invert
100
- * @return {Runner} for chaining
101
103
  * @api public
104
+ * @public
105
+ * @memberof Mocha.Runner
102
106
  * @param {RegExp} re
103
107
  * @param {boolean} invert
104
108
  * @return {Runner} Runner instance.
@@ -115,9 +119,9 @@ Runner.prototype.grep = function (re, invert) {
115
119
  * Returns the number of tests matching the grep search for the
116
120
  * given suite.
117
121
  *
118
- * @param {Suite} suite
119
- * @return {Number}
122
+ * @memberof Mocha.Runner
120
123
  * @api public
124
+ * @public
121
125
  * @param {Suite} suite
122
126
  * @return {number}
123
127
  */
@@ -161,9 +165,9 @@ Runner.prototype.globalProps = function () {
161
165
  /**
162
166
  * Allow the given `arr` of globals.
163
167
  *
164
- * @param {Array} arr
165
- * @return {Runner} for chaining
166
168
  * @api public
169
+ * @public
170
+ * @memberof Mocha.Runner
167
171
  * @param {Array} arr
168
172
  * @return {Runner} Runner instance.
169
173
  */
@@ -265,10 +269,10 @@ Runner.prototype.failHook = function (hook, err) {
265
269
  hook.title = hook.originalTitle + ' for "' + hook.ctx.currentTest.title + '"';
266
270
  }
267
271
 
268
- this.fail(hook, err);
269
272
  if (this.suite.bail()) {
270
273
  this.emit('end');
271
274
  }
275
+ this.fail(hook, err);
272
276
  };
273
277
 
274
278
  /**
@@ -804,9 +808,9 @@ function cleanSuiteReferences (suite) {
804
808
  * Run the root suite and invoke `fn(failures)`
805
809
  * on completion.
806
810
  *
807
- * @param {Function} fn
808
- * @return {Runner} for chaining
809
811
  * @api public
812
+ * @public
813
+ * @memberof Mocha.Runner
810
814
  * @param {Function} fn
811
815
  * @return {Runner} Runner instance.
812
816
  */
@@ -863,6 +867,8 @@ Runner.prototype.run = function (fn) {
863
867
  /**
864
868
  * Cleanly abort execution.
865
869
  *
870
+ * @memberof Mocha.Runner
871
+ * @public
866
872
  * @api public
867
873
  * @return {Runner} Runner instance.
868
874
  */
package/lib/suite.js CHANGED
@@ -1,9 +1,11 @@
1
1
  'use strict';
2
+ /**
3
+ * @module Suite
4
+ */
2
5
 
3
6
  /**
4
7
  * Module dependencies.
5
8
  */
6
-
7
9
  var EventEmitter = require('events').EventEmitter;
8
10
  var Hook = require('./hook');
9
11
  var utils = require('./utils');
@@ -22,6 +24,8 @@ exports = module.exports = Suite;
22
24
  * with the same title is already present, that suite is returned to provide
23
25
  * nicer reporter and more flexible meta-testing.
24
26
  *
27
+ * @memberof Mocha
28
+ * @public
25
29
  * @api public
26
30
  * @param {Suite} parent
27
31
  * @param {string} title
@@ -36,9 +40,11 @@ exports.create = function (parent, title) {
36
40
  };
37
41
 
38
42
  /**
39
- * Initialize a new `Suite` with the given `title` and `ctx`.
43
+ * Initialize a new `Suite` with the given `title` and `ctx`. Derived from [EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter)
40
44
  *
41
- * @api private
45
+ * @memberof Mocha
46
+ * @public
47
+ * @class
42
48
  * @param {string} title
43
49
  * @param {Context} parentContext
44
50
  */
@@ -189,6 +195,25 @@ Suite.prototype.isPending = function () {
189
195
  return this.pending || (this.parent && this.parent.isPending());
190
196
  };
191
197
 
198
+ /**
199
+ * Generic hook-creator.
200
+ * @private
201
+ * @param {string} title - Title of hook
202
+ * @param {Function} fn - Hook callback
203
+ * @returns {Hook} A new hook
204
+ */
205
+ Suite.prototype._createHook = function (title, fn) {
206
+ var hook = new Hook(title, fn);
207
+ hook.parent = this;
208
+ hook.timeout(this.timeout());
209
+ hook.retries(this.retries());
210
+ hook.enableTimeouts(this.enableTimeouts());
211
+ hook.slow(this.slow());
212
+ hook.ctx = this.ctx;
213
+ hook.file = this.file;
214
+ return hook;
215
+ };
216
+
192
217
  /**
193
218
  * Run `fn(test[, done])` before running tests.
194
219
  *
@@ -207,13 +232,7 @@ Suite.prototype.beforeAll = function (title, fn) {
207
232
  }
208
233
  title = '"before all" hook' + (title ? ': ' + title : '');
209
234
 
210
- var hook = new Hook(title, fn);
211
- hook.parent = this;
212
- hook.timeout(this.timeout());
213
- hook.retries(this.retries());
214
- hook.enableTimeouts(this.enableTimeouts());
215
- hook.slow(this.slow());
216
- hook.ctx = this.ctx;
235
+ var hook = this._createHook(title, fn);
217
236
  this._beforeAll.push(hook);
218
237
  this.emit('beforeAll', hook);
219
238
  return this;
@@ -237,13 +256,7 @@ Suite.prototype.afterAll = function (title, fn) {
237
256
  }
238
257
  title = '"after all" hook' + (title ? ': ' + title : '');
239
258
 
240
- var hook = new Hook(title, fn);
241
- hook.parent = this;
242
- hook.timeout(this.timeout());
243
- hook.retries(this.retries());
244
- hook.enableTimeouts(this.enableTimeouts());
245
- hook.slow(this.slow());
246
- hook.ctx = this.ctx;
259
+ var hook = this._createHook(title, fn);
247
260
  this._afterAll.push(hook);
248
261
  this.emit('afterAll', hook);
249
262
  return this;
@@ -267,13 +280,7 @@ Suite.prototype.beforeEach = function (title, fn) {
267
280
  }
268
281
  title = '"before each" hook' + (title ? ': ' + title : '');
269
282
 
270
- var hook = new Hook(title, fn);
271
- hook.parent = this;
272
- hook.timeout(this.timeout());
273
- hook.retries(this.retries());
274
- hook.enableTimeouts(this.enableTimeouts());
275
- hook.slow(this.slow());
276
- hook.ctx = this.ctx;
283
+ var hook = this._createHook(title, fn);
277
284
  this._beforeEach.push(hook);
278
285
  this.emit('beforeEach', hook);
279
286
  return this;
@@ -297,13 +304,7 @@ Suite.prototype.afterEach = function (title, fn) {
297
304
  }
298
305
  title = '"after each" hook' + (title ? ': ' + title : '');
299
306
 
300
- var hook = new Hook(title, fn);
301
- hook.parent = this;
302
- hook.timeout(this.timeout());
303
- hook.retries(this.retries());
304
- hook.enableTimeouts(this.enableTimeouts());
305
- hook.slow(this.slow());
306
- hook.ctx = this.ctx;
307
+ var hook = this._createHook(title, fn);
307
308
  this._afterEach.push(hook);
308
309
  this.emit('afterEach', hook);
309
310
  return this;
@@ -351,6 +352,8 @@ Suite.prototype.addTest = function (test) {
351
352
  * Return the full title generated by recursively concatenating the parent's
352
353
  * full title.
353
354
  *
355
+ * @memberof Mocha.Suite
356
+ * @public
354
357
  * @api public
355
358
  * @return {string}
356
359
  */
@@ -362,6 +365,8 @@ Suite.prototype.fullTitle = function () {
362
365
  * Return the title path generated by recursively concatenating the parent's
363
366
  * title path.
364
367
  *
368
+ * @memberof Mocha.Suite
369
+ * @public
365
370
  * @api public
366
371
  * @return {string}
367
372
  */
@@ -379,6 +384,8 @@ Suite.prototype.titlePath = function () {
379
384
  /**
380
385
  * Return the total number of tests.
381
386
  *
387
+ * @memberof Mocha.Suite
388
+ * @public
382
389
  * @api public
383
390
  * @return {number}
384
391
  */