mocha 8.1.3 → 8.2.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.
package/lib/utils.js CHANGED
@@ -9,11 +9,13 @@
9
9
  * Module dependencies.
10
10
  */
11
11
 
12
+ const {nanoid} = require('nanoid/non-secure');
12
13
  var path = require('path');
13
14
  var util = require('util');
14
15
  var he = require('he');
16
+ const errors = require('./errors');
15
17
 
16
- var assign = (exports.assign = require('object.assign').getPolyfill());
18
+ const MOCHA_ID_PROP_NAME = '__mocha_id__';
17
19
 
18
20
  /**
19
21
  * Inherit the prototype methods from one constructor into another.
@@ -127,19 +129,21 @@ function emptyRepresentation(value, typeHint) {
127
129
  * @param {*} value The value to test.
128
130
  * @returns {string} Computed type
129
131
  * @example
130
- * type({}) // 'object'
131
- * type([]) // 'array'
132
- * type(1) // 'number'
133
- * type(false) // 'boolean'
134
- * type(Infinity) // 'number'
135
- * type(null) // 'null'
136
- * type(new Date()) // 'date'
137
- * type(/foo/) // 'regexp'
138
- * type('type') // 'string'
139
- * type(global) // 'global'
140
- * type(new String('foo') // 'object'
132
+ * canonicalType({}) // 'object'
133
+ * canonicalType([]) // 'array'
134
+ * canonicalType(1) // 'number'
135
+ * canonicalType(false) // 'boolean'
136
+ * canonicalType(Infinity) // 'number'
137
+ * canonicalType(null) // 'null'
138
+ * canonicalType(new Date()) // 'date'
139
+ * canonicalType(/foo/) // 'regexp'
140
+ * canonicalType('type') // 'string'
141
+ * canonicalType(global) // 'global'
142
+ * canonicalType(new String('foo') // 'object'
143
+ * canonicalType(async function() {}) // 'asyncfunction'
144
+ * canonicalType(await import(name)) // 'module'
141
145
  */
142
- var type = (exports.type = function type(value) {
146
+ var canonicalType = (exports.canonicalType = function canonicalType(value) {
143
147
  if (value === undefined) {
144
148
  return 'undefined';
145
149
  } else if (value === null) {
@@ -153,6 +157,47 @@ var type = (exports.type = function type(value) {
153
157
  .toLowerCase();
154
158
  });
155
159
 
160
+ /**
161
+ *
162
+ * Returns a general type or data structure of a variable
163
+ * @private
164
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures
165
+ * @param {*} value The value to test.
166
+ * @returns {string} One of undefined, boolean, number, string, bigint, symbol, object
167
+ * @example
168
+ * type({}) // 'object'
169
+ * type([]) // 'array'
170
+ * type(1) // 'number'
171
+ * type(false) // 'boolean'
172
+ * type(Infinity) // 'number'
173
+ * type(null) // 'null'
174
+ * type(new Date()) // 'object'
175
+ * type(/foo/) // 'object'
176
+ * type('type') // 'string'
177
+ * type(global) // 'object'
178
+ * type(new String('foo') // 'string'
179
+ */
180
+ exports.type = function type(value) {
181
+ // Null is special
182
+ if (value === null) return 'null';
183
+ const primitives = new Set([
184
+ 'undefined',
185
+ 'boolean',
186
+ 'number',
187
+ 'string',
188
+ 'bigint',
189
+ 'symbol'
190
+ ]);
191
+ const _type = typeof value;
192
+ if (_type === 'function') return _type;
193
+ if (primitives.has(_type)) return _type;
194
+ if (value instanceof String) return 'string';
195
+ if (value instanceof Error) return 'error';
196
+ if (Array.isArray(value)) return 'array';
197
+
198
+ return _type;
199
+ };
200
+
156
201
  /**
157
202
  * Stringify `value`. Different behavior depending on type of value:
158
203
  *
@@ -169,7 +214,7 @@ var type = (exports.type = function type(value) {
169
214
  * @return {string}
170
215
  */
171
216
  exports.stringify = function(value) {
172
- var typeHint = type(value);
217
+ var typeHint = canonicalType(value);
173
218
 
174
219
  if (!~['object', 'array', 'function'].indexOf(typeHint)) {
175
220
  if (typeHint === 'buffer') {
@@ -235,7 +280,7 @@ function jsonStringify(object, spaces, depth) {
235
280
  }
236
281
 
237
282
  function _stringify(val) {
238
- switch (type(val)) {
283
+ switch (canonicalType(val)) {
239
284
  case 'null':
240
285
  case 'undefined':
241
286
  val = '[' + val + ']';
@@ -316,7 +361,7 @@ exports.canonicalize = function canonicalize(value, stack, typeHint) {
316
361
  /* eslint-disable no-unused-vars */
317
362
  var prop;
318
363
  /* eslint-enable no-unused-vars */
319
- typeHint = typeHint || type(value);
364
+ typeHint = typeHint || canonicalType(value);
320
365
  function withStack(value, fn) {
321
366
  stack.push(value);
322
367
  fn();
@@ -343,7 +388,7 @@ exports.canonicalize = function canonicalize(value, stack, typeHint) {
343
388
  });
344
389
  break;
345
390
  case 'function':
346
- /* eslint-disable guard-for-in */
391
+ /* eslint-disable-next-line no-unused-vars */
347
392
  for (prop in value) {
348
393
  canonicalizedObj = {};
349
394
  break;
@@ -378,50 +423,6 @@ exports.canonicalize = function canonicalize(value, stack, typeHint) {
378
423
  return canonicalizedObj;
379
424
  };
380
425
 
381
- /**
382
- * process.emitWarning or a polyfill
383
- * @see https://nodejs.org/api/process.html#process_process_emitwarning_warning_options
384
- * @ignore
385
- */
386
- function emitWarning(msg, type) {
387
- if (process.emitWarning) {
388
- process.emitWarning(msg, type);
389
- } else {
390
- process.nextTick(function() {
391
- console.warn(type + ': ' + msg);
392
- });
393
- }
394
- }
395
-
396
- /**
397
- * Show a deprecation warning. Each distinct message is only displayed once.
398
- * Ignores empty messages.
399
- *
400
- * @param {string} [msg] - Warning to print
401
- * @private
402
- */
403
- exports.deprecate = function deprecate(msg) {
404
- msg = String(msg);
405
- if (msg && !deprecate.cache[msg]) {
406
- deprecate.cache[msg] = true;
407
- emitWarning(msg, 'DeprecationWarning');
408
- }
409
- };
410
- exports.deprecate.cache = {};
411
-
412
- /**
413
- * Show a generic warning.
414
- * Ignores empty messages.
415
- *
416
- * @param {string} [msg] - Warning to print
417
- * @private
418
- */
419
- exports.warn = function warn(msg) {
420
- if (msg) {
421
- emitWarning(msg);
422
- }
423
- };
424
-
425
426
  /**
426
427
  * @summary
427
428
  * This Filter based on `mocha-clean` module.(see: `github.com/rstacruz/mocha-clean`)
@@ -575,7 +576,7 @@ exports.noop = function() {};
575
576
  * @returns {Object} An object with no prototype, having `...obj` properties
576
577
  */
577
578
  exports.createMap = function(obj) {
578
- return assign.apply(
579
+ return Object.assign.apply(
579
580
  null,
580
581
  [Object.create(null)].concat(Array.prototype.slice.call(arguments))
581
582
  );
@@ -594,7 +595,7 @@ exports.createMap = function(obj) {
594
595
  * @throws {TypeError} if argument is not a non-empty object.
595
596
  */
596
597
  exports.defineConstants = function(obj) {
597
- if (type(obj) !== 'object' || !Object.keys(obj).length) {
598
+ if (canonicalType(obj) !== 'object' || !Object.keys(obj).length) {
598
599
  throw new TypeError('Invalid argument; expected a non-empty object');
599
600
  }
600
601
  return Object.freeze(exports.createMap(obj));
@@ -665,12 +666,67 @@ exports.isBrowser = function isBrowser() {
665
666
  */
666
667
  exports.lookupFiles = (...args) => {
667
668
  if (exports.isBrowser()) {
668
- throw require('./errors').createUnsupportedError(
669
+ throw errors.createUnsupportedError(
669
670
  'lookupFiles() is only supported in Node.js!'
670
671
  );
671
672
  }
672
- exports.deprecate(
673
+ errors.deprecate(
673
674
  '`lookupFiles()` in module `mocha/lib/utils` has moved to module `mocha/lib/cli` and will be removed in the next major revision of Mocha'
674
675
  );
675
676
  return require('./cli').lookupFiles(...args);
676
677
  };
678
+
679
+ /*
680
+ * Casts `value` to an array; useful for optionally accepting array parameters
681
+ *
682
+ * It follows these rules, depending on `value`. If `value` is...
683
+ * 1. `undefined`: return an empty Array
684
+ * 2. `null`: return an array with a single `null` element
685
+ * 3. Any other object: return the value of `Array.from()` _if_ the object is iterable
686
+ * 4. otherwise: return an array with a single element, `value`
687
+ * @param {*} value - Something to cast to an Array
688
+ * @returns {Array<*>}
689
+ */
690
+ exports.castArray = function castArray(value) {
691
+ if (value === undefined) {
692
+ return [];
693
+ }
694
+ if (value === null) {
695
+ return [null];
696
+ }
697
+ if (
698
+ typeof value === 'object' &&
699
+ (typeof value[Symbol.iterator] === 'function' || value.length !== undefined)
700
+ ) {
701
+ return Array.from(value);
702
+ }
703
+ return [value];
704
+ };
705
+
706
+ exports.constants = exports.defineConstants({
707
+ MOCHA_ID_PROP_NAME
708
+ });
709
+
710
+ /**
711
+ * Creates a new unique identifier
712
+ * @returns {string} Unique identifier
713
+ */
714
+ exports.uniqueID = () => nanoid();
715
+
716
+ exports.assignNewMochaID = obj => {
717
+ const id = exports.uniqueID();
718
+ Object.defineProperty(obj, MOCHA_ID_PROP_NAME, {
719
+ get() {
720
+ return id;
721
+ }
722
+ });
723
+ return obj;
724
+ };
725
+
726
+ /**
727
+ * Retrieves a Mocha ID from an object, if present.
728
+ * @param {*} [obj] - Object
729
+ * @returns {string|void}
730
+ */
731
+ exports.getMochaID = obj =>
732
+ obj && typeof obj === 'object' ? obj[MOCHA_ID_PROP_NAME] : undefined;