mocha 9.1.2

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.
Files changed (76) hide show
  1. package/CHANGELOG.md +1015 -0
  2. package/LICENSE +22 -0
  3. package/README.md +70 -0
  4. package/assets/growl/error.png +0 -0
  5. package/assets/growl/ok.png +0 -0
  6. package/bin/_mocha +10 -0
  7. package/bin/mocha +142 -0
  8. package/browser-entry.js +216 -0
  9. package/index.js +3 -0
  10. package/lib/browser/growl.js +169 -0
  11. package/lib/browser/highlight-tags.js +39 -0
  12. package/lib/browser/parse-query.js +24 -0
  13. package/lib/browser/progress.js +123 -0
  14. package/lib/browser/template.html +20 -0
  15. package/lib/cli/cli.js +89 -0
  16. package/lib/cli/collect-files.js +92 -0
  17. package/lib/cli/commands.js +13 -0
  18. package/lib/cli/config.js +105 -0
  19. package/lib/cli/index.js +3 -0
  20. package/lib/cli/init.js +36 -0
  21. package/lib/cli/lookup-files.js +145 -0
  22. package/lib/cli/node-flags.js +85 -0
  23. package/lib/cli/one-and-dones.js +69 -0
  24. package/lib/cli/options.js +261 -0
  25. package/lib/cli/run-helpers.js +243 -0
  26. package/lib/cli/run-option-metadata.js +117 -0
  27. package/lib/cli/run.js +379 -0
  28. package/lib/cli/watch-run.js +380 -0
  29. package/lib/context.js +86 -0
  30. package/lib/errors.js +563 -0
  31. package/lib/hook.js +89 -0
  32. package/lib/interfaces/bdd.js +111 -0
  33. package/lib/interfaces/common.js +193 -0
  34. package/lib/interfaces/exports.js +60 -0
  35. package/lib/interfaces/index.js +6 -0
  36. package/lib/interfaces/qunit.js +98 -0
  37. package/lib/interfaces/tdd.js +106 -0
  38. package/lib/mocha.js +1374 -0
  39. package/lib/mocharc.json +10 -0
  40. package/lib/nodejs/buffered-worker-pool.js +172 -0
  41. package/lib/nodejs/esm-utils.js +109 -0
  42. package/lib/nodejs/file-unloader.js +15 -0
  43. package/lib/nodejs/growl.js +137 -0
  44. package/lib/nodejs/parallel-buffered-runner.js +433 -0
  45. package/lib/nodejs/reporters/parallel-buffered.js +165 -0
  46. package/lib/nodejs/serializer.js +412 -0
  47. package/lib/nodejs/worker.js +151 -0
  48. package/lib/pending.js +16 -0
  49. package/lib/plugin-loader.js +286 -0
  50. package/lib/reporters/base.js +537 -0
  51. package/lib/reporters/doc.js +95 -0
  52. package/lib/reporters/dot.js +81 -0
  53. package/lib/reporters/html.js +390 -0
  54. package/lib/reporters/index.js +19 -0
  55. package/lib/reporters/json-stream.js +92 -0
  56. package/lib/reporters/json.js +162 -0
  57. package/lib/reporters/landing.js +116 -0
  58. package/lib/reporters/list.js +78 -0
  59. package/lib/reporters/markdown.js +112 -0
  60. package/lib/reporters/min.js +52 -0
  61. package/lib/reporters/nyan.js +276 -0
  62. package/lib/reporters/progress.js +104 -0
  63. package/lib/reporters/spec.js +99 -0
  64. package/lib/reporters/tap.js +293 -0
  65. package/lib/reporters/xunit.js +217 -0
  66. package/lib/runnable.js +476 -0
  67. package/lib/runner.js +1269 -0
  68. package/lib/stats-collector.js +83 -0
  69. package/lib/suite.js +695 -0
  70. package/lib/test.js +113 -0
  71. package/lib/utils.js +641 -0
  72. package/mocha-es2018.js +19816 -0
  73. package/mocha.css +325 -0
  74. package/mocha.js +30844 -0
  75. package/mocha.js.map +1 -0
  76. package/package.json +200 -0
@@ -0,0 +1,476 @@
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
+ const {
9
+ createInvalidExceptionError,
10
+ createMultipleDoneError,
11
+ createTimeoutError
12
+ } = require('./errors');
13
+
14
+ /**
15
+ * Save timer references to avoid Sinon interfering (see GH-237).
16
+ * @private
17
+ */
18
+ var Date = global.Date;
19
+ var setTimeout = global.setTimeout;
20
+ var clearTimeout = global.clearTimeout;
21
+ var toString = Object.prototype.toString;
22
+
23
+ module.exports = Runnable;
24
+
25
+ /**
26
+ * Initialize a new `Runnable` with the given `title` and callback `fn`.
27
+ *
28
+ * @class
29
+ * @extends external:EventEmitter
30
+ * @public
31
+ * @param {String} title
32
+ * @param {Function} fn
33
+ */
34
+ function Runnable(title, fn) {
35
+ this.title = title;
36
+ this.fn = fn;
37
+ this.body = (fn || '').toString();
38
+ this.async = fn && fn.length;
39
+ this.sync = !this.async;
40
+ this._timeout = 2000;
41
+ this._slow = 75;
42
+ this._retries = -1;
43
+ utils.assignNewMochaID(this);
44
+ Object.defineProperty(this, 'id', {
45
+ get() {
46
+ return utils.getMochaID(this);
47
+ }
48
+ });
49
+ this.reset();
50
+ }
51
+
52
+ /**
53
+ * Inherit from `EventEmitter.prototype`.
54
+ */
55
+ utils.inherits(Runnable, EventEmitter);
56
+
57
+ /**
58
+ * Resets the state initially or for a next run.
59
+ */
60
+ Runnable.prototype.reset = function() {
61
+ this.timedOut = false;
62
+ this._currentRetry = 0;
63
+ this.pending = false;
64
+ delete this.state;
65
+ delete this.err;
66
+ };
67
+
68
+ /**
69
+ * Get current timeout value in msecs.
70
+ *
71
+ * @private
72
+ * @returns {number} current timeout threshold value
73
+ */
74
+ /**
75
+ * @summary
76
+ * Set timeout threshold value (msecs).
77
+ *
78
+ * @description
79
+ * A string argument can use shorthand (e.g., "2s") and will be converted.
80
+ * The value will be clamped to range [<code>0</code>, <code>2^<sup>31</sup>-1</code>].
81
+ * If clamped value matches either range endpoint, timeouts will be disabled.
82
+ *
83
+ * @private
84
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout#Maximum_delay_value}
85
+ * @param {number|string} ms - Timeout threshold value.
86
+ * @returns {Runnable} this
87
+ * @chainable
88
+ */
89
+ Runnable.prototype.timeout = function(ms) {
90
+ if (!arguments.length) {
91
+ return this._timeout;
92
+ }
93
+ if (typeof ms === 'string') {
94
+ ms = milliseconds(ms);
95
+ }
96
+
97
+ // Clamp to range
98
+ var INT_MAX = Math.pow(2, 31) - 1;
99
+ var range = [0, INT_MAX];
100
+ ms = utils.clamp(ms, range);
101
+
102
+ // see #1652 for reasoning
103
+ if (ms === range[0] || ms === range[1]) {
104
+ this._timeout = 0;
105
+ } else {
106
+ this._timeout = ms;
107
+ }
108
+ debug('timeout %d', this._timeout);
109
+
110
+ if (this.timer) {
111
+ this.resetTimeout();
112
+ }
113
+ return this;
114
+ };
115
+
116
+ /**
117
+ * Set or get slow `ms`.
118
+ *
119
+ * @private
120
+ * @param {number|string} ms
121
+ * @return {Runnable|number} ms or Runnable instance.
122
+ */
123
+ Runnable.prototype.slow = function(ms) {
124
+ if (!arguments.length || typeof ms === 'undefined') {
125
+ return this._slow;
126
+ }
127
+ if (typeof ms === 'string') {
128
+ ms = milliseconds(ms);
129
+ }
130
+ debug('slow %d', ms);
131
+ this._slow = ms;
132
+ return this;
133
+ };
134
+
135
+ /**
136
+ * Halt and mark as pending.
137
+ *
138
+ * @memberof Mocha.Runnable
139
+ * @public
140
+ */
141
+ Runnable.prototype.skip = function() {
142
+ this.pending = true;
143
+ throw new Pending('sync skip; aborting execution');
144
+ };
145
+
146
+ /**
147
+ * Check if this runnable or its parent suite is marked as pending.
148
+ *
149
+ * @private
150
+ */
151
+ Runnable.prototype.isPending = function() {
152
+ return this.pending || (this.parent && this.parent.isPending());
153
+ };
154
+
155
+ /**
156
+ * Return `true` if this Runnable has failed.
157
+ * @return {boolean}
158
+ * @private
159
+ */
160
+ Runnable.prototype.isFailed = function() {
161
+ return !this.isPending() && this.state === constants.STATE_FAILED;
162
+ };
163
+
164
+ /**
165
+ * Return `true` if this Runnable has passed.
166
+ * @return {boolean}
167
+ * @private
168
+ */
169
+ Runnable.prototype.isPassed = function() {
170
+ return !this.isPending() && this.state === constants.STATE_PASSED;
171
+ };
172
+
173
+ /**
174
+ * Set or get number of retries.
175
+ *
176
+ * @private
177
+ */
178
+ Runnable.prototype.retries = function(n) {
179
+ if (!arguments.length) {
180
+ return this._retries;
181
+ }
182
+ this._retries = n;
183
+ };
184
+
185
+ /**
186
+ * Set or get current retry
187
+ *
188
+ * @private
189
+ */
190
+ Runnable.prototype.currentRetry = function(n) {
191
+ if (!arguments.length) {
192
+ return this._currentRetry;
193
+ }
194
+ this._currentRetry = n;
195
+ };
196
+
197
+ /**
198
+ * Return the full title generated by recursively concatenating the parent's
199
+ * full title.
200
+ *
201
+ * @memberof Mocha.Runnable
202
+ * @public
203
+ * @return {string}
204
+ */
205
+ Runnable.prototype.fullTitle = function() {
206
+ return this.titlePath().join(' ');
207
+ };
208
+
209
+ /**
210
+ * Return the title path generated by concatenating the parent's title path with the title.
211
+ *
212
+ * @memberof Mocha.Runnable
213
+ * @public
214
+ * @return {string}
215
+ */
216
+ Runnable.prototype.titlePath = function() {
217
+ return this.parent.titlePath().concat([this.title]);
218
+ };
219
+
220
+ /**
221
+ * Clear the timeout.
222
+ *
223
+ * @private
224
+ */
225
+ Runnable.prototype.clearTimeout = function() {
226
+ clearTimeout(this.timer);
227
+ };
228
+
229
+ /**
230
+ * Reset the timeout.
231
+ *
232
+ * @private
233
+ */
234
+ Runnable.prototype.resetTimeout = function() {
235
+ var self = this;
236
+ var ms = this.timeout();
237
+
238
+ if (ms === 0) {
239
+ return;
240
+ }
241
+ this.clearTimeout();
242
+ this.timer = setTimeout(function() {
243
+ if (self.timeout() === 0) {
244
+ return;
245
+ }
246
+ self.callback(self._timeoutError(ms));
247
+ self.timedOut = true;
248
+ }, ms);
249
+ };
250
+
251
+ /**
252
+ * Set or get a list of whitelisted globals for this test run.
253
+ *
254
+ * @private
255
+ * @param {string[]} globals
256
+ */
257
+ Runnable.prototype.globals = function(globals) {
258
+ if (!arguments.length) {
259
+ return this._allowedGlobals;
260
+ }
261
+ this._allowedGlobals = globals;
262
+ };
263
+
264
+ /**
265
+ * Run the test and invoke `fn(err)`.
266
+ *
267
+ * @param {Function} fn
268
+ * @private
269
+ */
270
+ Runnable.prototype.run = function(fn) {
271
+ var self = this;
272
+ var start = new Date();
273
+ var ctx = this.ctx;
274
+ var finished;
275
+ var errorWasHandled = false;
276
+
277
+ if (this.isPending()) return fn();
278
+
279
+ // Sometimes the ctx exists, but it is not runnable
280
+ if (ctx && ctx.runnable) {
281
+ ctx.runnable(this);
282
+ }
283
+
284
+ // called multiple times
285
+ function multiple(err) {
286
+ if (errorWasHandled) {
287
+ return;
288
+ }
289
+ errorWasHandled = true;
290
+ self.emit('error', createMultipleDoneError(self, err));
291
+ }
292
+
293
+ // finished
294
+ function done(err) {
295
+ var ms = self.timeout();
296
+ if (self.timedOut) {
297
+ return;
298
+ }
299
+
300
+ if (finished) {
301
+ return multiple(err);
302
+ }
303
+
304
+ self.clearTimeout();
305
+ self.duration = new Date() - start;
306
+ finished = true;
307
+ if (!err && self.duration > ms && ms > 0) {
308
+ err = self._timeoutError(ms);
309
+ }
310
+ fn(err);
311
+ }
312
+
313
+ // for .resetTimeout() and Runner#uncaught()
314
+ this.callback = done;
315
+
316
+ if (this.fn && typeof this.fn.call !== 'function') {
317
+ done(
318
+ new TypeError(
319
+ 'A runnable must be passed a function as its second argument.'
320
+ )
321
+ );
322
+ return;
323
+ }
324
+
325
+ // explicit async with `done` argument
326
+ if (this.async) {
327
+ this.resetTimeout();
328
+
329
+ // allows skip() to be used in an explicit async context
330
+ this.skip = function asyncSkip() {
331
+ this.pending = true;
332
+ done();
333
+ // halt execution, the uncaught handler will ignore the failure.
334
+ throw new Pending('async skip; aborting execution');
335
+ };
336
+
337
+ try {
338
+ callFnAsync(this.fn);
339
+ } catch (err) {
340
+ // handles async runnables which actually run synchronously
341
+ errorWasHandled = true;
342
+ if (err instanceof Pending) {
343
+ return; // done() is already called in this.skip()
344
+ } else if (this.allowUncaught) {
345
+ throw err;
346
+ }
347
+ done(Runnable.toValueOrError(err));
348
+ }
349
+ return;
350
+ }
351
+
352
+ // sync or promise-returning
353
+ try {
354
+ callFn(this.fn);
355
+ } catch (err) {
356
+ errorWasHandled = true;
357
+ if (err instanceof Pending) {
358
+ return done();
359
+ } else if (this.allowUncaught) {
360
+ throw err;
361
+ }
362
+ done(Runnable.toValueOrError(err));
363
+ }
364
+
365
+ function callFn(fn) {
366
+ var result = fn.call(ctx);
367
+ if (result && typeof result.then === 'function') {
368
+ self.resetTimeout();
369
+ result.then(
370
+ function() {
371
+ done();
372
+ // Return null so libraries like bluebird do not warn about
373
+ // subsequently constructed Promises.
374
+ return null;
375
+ },
376
+ function(reason) {
377
+ done(reason || new Error('Promise rejected with no or falsy reason'));
378
+ }
379
+ );
380
+ } else {
381
+ if (self.asyncOnly) {
382
+ return done(
383
+ new Error(
384
+ '--async-only option in use without declaring `done()` or returning a promise'
385
+ )
386
+ );
387
+ }
388
+
389
+ done();
390
+ }
391
+ }
392
+
393
+ function callFnAsync(fn) {
394
+ var result = fn.call(ctx, function(err) {
395
+ if (err instanceof Error || toString.call(err) === '[object Error]') {
396
+ return done(err);
397
+ }
398
+ if (err) {
399
+ if (Object.prototype.toString.call(err) === '[object Object]') {
400
+ return done(
401
+ new Error('done() invoked with non-Error: ' + JSON.stringify(err))
402
+ );
403
+ }
404
+ return done(new Error('done() invoked with non-Error: ' + err));
405
+ }
406
+ if (result && utils.isPromise(result)) {
407
+ return done(
408
+ new Error(
409
+ 'Resolution method is overspecified. Specify a callback *or* return a Promise; not both.'
410
+ )
411
+ );
412
+ }
413
+
414
+ done();
415
+ });
416
+ }
417
+ };
418
+
419
+ /**
420
+ * Instantiates a "timeout" error
421
+ *
422
+ * @param {number} ms - Timeout (in milliseconds)
423
+ * @returns {Error} a "timeout" error
424
+ * @private
425
+ */
426
+ Runnable.prototype._timeoutError = function(ms) {
427
+ let msg = `Timeout of ${ms}ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.`;
428
+ if (this.file) {
429
+ msg += ' (' + this.file + ')';
430
+ }
431
+ return createTimeoutError(msg, ms, this.file);
432
+ };
433
+
434
+ var constants = utils.defineConstants(
435
+ /**
436
+ * {@link Runnable}-related constants.
437
+ * @public
438
+ * @memberof Runnable
439
+ * @readonly
440
+ * @static
441
+ * @alias constants
442
+ * @enum {string}
443
+ */
444
+ {
445
+ /**
446
+ * Value of `state` prop when a `Runnable` has failed
447
+ */
448
+ STATE_FAILED: 'failed',
449
+ /**
450
+ * Value of `state` prop when a `Runnable` has passed
451
+ */
452
+ STATE_PASSED: 'passed',
453
+ /**
454
+ * Value of `state` prop when a `Runnable` has been skipped by user
455
+ */
456
+ STATE_PENDING: 'pending'
457
+ }
458
+ );
459
+
460
+ /**
461
+ * Given `value`, return identity if truthy, otherwise create an "invalid exception" error and return that.
462
+ * @param {*} [value] - Value to return, if present
463
+ * @returns {*|Error} `value`, otherwise an `Error`
464
+ * @private
465
+ */
466
+ Runnable.toValueOrError = function(value) {
467
+ return (
468
+ value ||
469
+ createInvalidExceptionError(
470
+ 'Runnable failed with falsy or undefined exception. Please throw an Error instead.',
471
+ value
472
+ )
473
+ );
474
+ };
475
+
476
+ Runnable.constants = constants;