mocha 8.1.2 → 8.3.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 + ']';
@@ -253,6 +298,9 @@ function jsonStringify(object, spaces, depth) {
253
298
  ? '-0'
254
299
  : val.toString();
255
300
  break;
301
+ case 'bigint':
302
+ val = val.toString() + 'n';
303
+ break;
256
304
  case 'date':
257
305
  var sDate = isNaN(val.getTime()) ? val.toString() : val.toISOString();
258
306
  val = '[Date: ' + sDate + ']';
@@ -316,7 +364,7 @@ exports.canonicalize = function canonicalize(value, stack, typeHint) {
316
364
  /* eslint-disable no-unused-vars */
317
365
  var prop;
318
366
  /* eslint-enable no-unused-vars */
319
- typeHint = typeHint || type(value);
367
+ typeHint = typeHint || canonicalType(value);
320
368
  function withStack(value, fn) {
321
369
  stack.push(value);
322
370
  fn();
@@ -343,7 +391,7 @@ exports.canonicalize = function canonicalize(value, stack, typeHint) {
343
391
  });
344
392
  break;
345
393
  case 'function':
346
- /* eslint-disable guard-for-in */
394
+ /* eslint-disable-next-line no-unused-vars */
347
395
  for (prop in value) {
348
396
  canonicalizedObj = {};
349
397
  break;
@@ -378,50 +426,6 @@ exports.canonicalize = function canonicalize(value, stack, typeHint) {
378
426
  return canonicalizedObj;
379
427
  };
380
428
 
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
429
  /**
426
430
  * @summary
427
431
  * This Filter based on `mocha-clean` module.(see: `github.com/rstacruz/mocha-clean`)
@@ -575,7 +579,7 @@ exports.noop = function() {};
575
579
  * @returns {Object} An object with no prototype, having `...obj` properties
576
580
  */
577
581
  exports.createMap = function(obj) {
578
- return assign.apply(
582
+ return Object.assign.apply(
579
583
  null,
580
584
  [Object.create(null)].concat(Array.prototype.slice.call(arguments))
581
585
  );
@@ -594,7 +598,7 @@ exports.createMap = function(obj) {
594
598
  * @throws {TypeError} if argument is not a non-empty object.
595
599
  */
596
600
  exports.defineConstants = function(obj) {
597
- if (type(obj) !== 'object' || !Object.keys(obj).length) {
601
+ if (canonicalType(obj) !== 'object' || !Object.keys(obj).length) {
598
602
  throw new TypeError('Invalid argument; expected a non-empty object');
599
603
  }
600
604
  return Object.freeze(exports.createMap(obj));
@@ -645,3 +649,87 @@ exports.cwd = function cwd() {
645
649
  exports.isBrowser = function isBrowser() {
646
650
  return Boolean(process.browser);
647
651
  };
652
+
653
+ /**
654
+ * Lookup file names at the given `path`.
655
+ *
656
+ * @description
657
+ * Filenames are returned in _traversal_ order by the OS/filesystem.
658
+ * **Make no assumption that the names will be sorted in any fashion.**
659
+ *
660
+ * @public
661
+ * @alias module:lib/cli.lookupFiles
662
+ * @param {string} filepath - Base path to start searching from.
663
+ * @param {string[]} [extensions=[]] - File extensions to look for.
664
+ * @param {boolean} [recursive=false] - Whether to recurse into subdirectories.
665
+ * @return {string[]} An array of paths.
666
+ * @throws {Error} if no files match pattern.
667
+ * @throws {TypeError} if `filepath` is directory and `extensions` not provided.
668
+ * @deprecated Moved to {@link module:lib/cli.lookupFiles}
669
+ */
670
+ exports.lookupFiles = (...args) => {
671
+ if (exports.isBrowser()) {
672
+ throw errors.createUnsupportedError(
673
+ 'lookupFiles() is only supported in Node.js!'
674
+ );
675
+ }
676
+ errors.deprecate(
677
+ '`lookupFiles()` in module `mocha/lib/utils` has moved to module `mocha/lib/cli` and will be removed in the next major revision of Mocha'
678
+ );
679
+ return require('./cli').lookupFiles(...args);
680
+ };
681
+
682
+ /*
683
+ * Casts `value` to an array; useful for optionally accepting array parameters
684
+ *
685
+ * It follows these rules, depending on `value`. If `value` is...
686
+ * 1. `undefined`: return an empty Array
687
+ * 2. `null`: return an array with a single `null` element
688
+ * 3. Any other object: return the value of `Array.from()` _if_ the object is iterable
689
+ * 4. otherwise: return an array with a single element, `value`
690
+ * @param {*} value - Something to cast to an Array
691
+ * @returns {Array<*>}
692
+ */
693
+ exports.castArray = function castArray(value) {
694
+ if (value === undefined) {
695
+ return [];
696
+ }
697
+ if (value === null) {
698
+ return [null];
699
+ }
700
+ if (
701
+ typeof value === 'object' &&
702
+ (typeof value[Symbol.iterator] === 'function' || value.length !== undefined)
703
+ ) {
704
+ return Array.from(value);
705
+ }
706
+ return [value];
707
+ };
708
+
709
+ exports.constants = exports.defineConstants({
710
+ MOCHA_ID_PROP_NAME
711
+ });
712
+
713
+ /**
714
+ * Creates a new unique identifier
715
+ * @returns {string} Unique identifier
716
+ */
717
+ exports.uniqueID = () => nanoid();
718
+
719
+ exports.assignNewMochaID = obj => {
720
+ const id = exports.uniqueID();
721
+ Object.defineProperty(obj, MOCHA_ID_PROP_NAME, {
722
+ get() {
723
+ return id;
724
+ }
725
+ });
726
+ return obj;
727
+ };
728
+
729
+ /**
730
+ * Retrieves a Mocha ID from an object, if present.
731
+ * @param {*} [obj] - Object
732
+ * @returns {string|void}
733
+ */
734
+ exports.getMochaID = obj =>
735
+ obj && typeof obj === 'object' ? obj[MOCHA_ID_PROP_NAME] : undefined;