mocha 8.0.1 → 8.1.3

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,14 +9,9 @@
9
9
  * Module dependencies.
10
10
  */
11
11
 
12
- var fs = require('fs');
13
12
  var path = require('path');
14
13
  var util = require('util');
15
- var glob = require('glob');
16
14
  var he = require('he');
17
- var errors = require('./errors');
18
- var createNoFilesMatchPatternError = errors.createNoFilesMatchPatternError;
19
- var createMissingArgumentError = errors.createMissingArgumentError;
20
15
 
21
16
  var assign = (exports.assign = require('object.assign').getPolyfill());
22
17
 
@@ -96,67 +91,6 @@ exports.clean = function(str) {
96
91
  return str.trim();
97
92
  };
98
93
 
99
- /**
100
- * Parse the given `qs`.
101
- *
102
- * @private
103
- * @param {string} qs
104
- * @return {Object}
105
- */
106
- exports.parseQuery = function(qs) {
107
- return qs
108
- .replace('?', '')
109
- .split('&')
110
- .reduce(function(obj, pair) {
111
- var i = pair.indexOf('=');
112
- var key = pair.slice(0, i);
113
- var val = pair.slice(++i);
114
-
115
- // Due to how the URLSearchParams API treats spaces
116
- obj[key] = decodeURIComponent(val.replace(/\+/g, '%20'));
117
-
118
- return obj;
119
- }, {});
120
- };
121
-
122
- /**
123
- * Highlight the given string of `js`.
124
- *
125
- * @private
126
- * @param {string} js
127
- * @return {string}
128
- */
129
- function highlight(js) {
130
- return js
131
- .replace(/</g, '&lt;')
132
- .replace(/>/g, '&gt;')
133
- .replace(/\/\/(.*)/gm, '<span class="comment">//$1</span>')
134
- .replace(/('.*?')/gm, '<span class="string">$1</span>')
135
- .replace(/(\d+\.\d+)/gm, '<span class="number">$1</span>')
136
- .replace(/(\d+)/gm, '<span class="number">$1</span>')
137
- .replace(
138
- /\bnew[ \t]+(\w+)/gm,
139
- '<span class="keyword">new</span> <span class="init">$1</span>'
140
- )
141
- .replace(
142
- /\b(function|new|throw|return|var|if|else)\b/gm,
143
- '<span class="keyword">$1</span>'
144
- );
145
- }
146
-
147
- /**
148
- * Highlight the contents of tag `name`.
149
- *
150
- * @private
151
- * @param {string} name
152
- */
153
- exports.highlightTags = function(name) {
154
- var code = document.getElementById('mocha').getElementsByTagName(name);
155
- for (var i = 0, len = code.length; i < len; ++i) {
156
- code[i].innerHTML = highlight(code[i].innerHTML);
157
- }
158
- };
159
-
160
94
  /**
161
95
  * If a value could have properties, and has none, this function is called,
162
96
  * which returns a string representation of the empty value.
@@ -444,140 +378,6 @@ exports.canonicalize = function canonicalize(value, stack, typeHint) {
444
378
  return canonicalizedObj;
445
379
  };
446
380
 
447
- /**
448
- * Determines if pathname has a matching file extension.
449
- *
450
- * @private
451
- * @param {string} pathname - Pathname to check for match.
452
- * @param {string[]} exts - List of file extensions (sans period).
453
- * @return {boolean} whether file extension matches.
454
- * @example
455
- * hasMatchingExtname('foo.html', ['js', 'css']); // => false
456
- */
457
- function hasMatchingExtname(pathname, exts) {
458
- var suffix = path.extname(pathname).slice(1);
459
- return exts.some(function(element) {
460
- return suffix === element;
461
- });
462
- }
463
-
464
- /**
465
- * Determines if pathname would be a "hidden" file (or directory) on UN*X.
466
- *
467
- * @description
468
- * On UN*X, pathnames beginning with a full stop (aka dot) are hidden during
469
- * typical usage. Dotfiles, plain-text configuration files, are prime examples.
470
- *
471
- * @see {@link http://xahlee.info/UnixResource_dir/writ/unix_origin_of_dot_filename.html|Origin of Dot File Names}
472
- *
473
- * @private
474
- * @param {string} pathname - Pathname to check for match.
475
- * @return {boolean} whether pathname would be considered a hidden file.
476
- * @example
477
- * isHiddenOnUnix('.profile'); // => true
478
- */
479
- function isHiddenOnUnix(pathname) {
480
- return path.basename(pathname)[0] === '.';
481
- }
482
-
483
- /**
484
- * Lookup file names at the given `path`.
485
- *
486
- * @description
487
- * Filenames are returned in _traversal_ order by the OS/filesystem.
488
- * **Make no assumption that the names will be sorted in any fashion.**
489
- *
490
- * @public
491
- * @param {string} filepath - Base path to start searching from.
492
- * @param {string[]} [extensions=[]] - File extensions to look for.
493
- * @param {boolean} [recursive=false] - Whether to recurse into subdirectories.
494
- * @return {string[]} An array of paths.
495
- * @throws {Error} if no files match pattern.
496
- * @throws {TypeError} if `filepath` is directory and `extensions` not provided.
497
- */
498
- exports.lookupFiles = function lookupFiles(filepath, extensions, recursive) {
499
- extensions = extensions || [];
500
- recursive = recursive || false;
501
- var files = [];
502
- var stat;
503
-
504
- if (!fs.existsSync(filepath)) {
505
- var pattern;
506
- if (glob.hasMagic(filepath)) {
507
- // Handle glob as is without extensions
508
- pattern = filepath;
509
- } else {
510
- // glob pattern e.g. 'filepath+(.js|.ts)'
511
- var strExtensions = extensions
512
- .map(function(v) {
513
- return '.' + v;
514
- })
515
- .join('|');
516
- pattern = filepath + '+(' + strExtensions + ')';
517
- }
518
- files = glob.sync(pattern, {nodir: true});
519
- if (!files.length) {
520
- throw createNoFilesMatchPatternError(
521
- 'Cannot find any files matching pattern ' + exports.dQuote(filepath),
522
- filepath
523
- );
524
- }
525
- return files;
526
- }
527
-
528
- // Handle file
529
- try {
530
- stat = fs.statSync(filepath);
531
- if (stat.isFile()) {
532
- return filepath;
533
- }
534
- } catch (err) {
535
- // ignore error
536
- return;
537
- }
538
-
539
- // Handle directory
540
- fs.readdirSync(filepath).forEach(function(dirent) {
541
- var pathname = path.join(filepath, dirent);
542
- var stat;
543
-
544
- try {
545
- stat = fs.statSync(pathname);
546
- if (stat.isDirectory()) {
547
- if (recursive) {
548
- files = files.concat(lookupFiles(pathname, extensions, recursive));
549
- }
550
- return;
551
- }
552
- } catch (err) {
553
- // ignore error
554
- return;
555
- }
556
- if (!extensions.length) {
557
- throw createMissingArgumentError(
558
- util.format(
559
- 'Argument %s required when argument %s is a directory',
560
- exports.sQuote('extensions'),
561
- exports.sQuote('filepath')
562
- ),
563
- 'extensions',
564
- 'array'
565
- );
566
- }
567
-
568
- if (
569
- !stat.isFile() ||
570
- !hasMatchingExtname(pathname, extensions) ||
571
- isHiddenOnUnix(pathname)
572
- ) {
573
- return;
574
- }
575
- files.push(pathname);
576
- });
577
-
578
- return files;
579
- };
580
-
581
381
  /**
582
382
  * process.emitWarning or a polyfill
583
383
  * @see https://nodejs.org/api/process.html#process_process_emitwarning_warning_options
@@ -708,7 +508,7 @@ exports.isPromise = function isPromise(value) {
708
508
  * Clamps a numeric value to an inclusive range.
709
509
  *
710
510
  * @param {number} value - Value to be clamped.
711
- * @param {numer[]} range - Two element array specifying [min, max] range.
511
+ * @param {number[]} range - Two element array specifying [min, max] range.
712
512
  * @returns {number} clamped value
713
513
  */
714
514
  exports.clamp = function clamp(value, range) {
@@ -768,9 +568,9 @@ exports.noop = function() {};
768
568
  * doesn't support it. Recommended for use in Mocha's public APIs.
769
569
  *
770
570
  * @public
771
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map|MDN:Map}
571
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#Custom_and_Null_objects|MDN:Map}
772
572
  * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create#Custom_and_Null_objects|MDN:Object.create - Custom objects}
773
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign|MDN:Object.assign}
573
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Custom_and_Null_objects|MDN:Object.assign}
774
574
  * @param {...*} [obj] - Arguments to `Object.assign()`.
775
575
  * @returns {Object} An object with no prototype, having `...obj` properties
776
576
  */
@@ -804,7 +604,7 @@ exports.defineConstants = function(obj) {
804
604
  * Whether current version of Node support ES modules
805
605
  *
806
606
  * @description
807
- * Versions prior to 10 did not support ES Modules, and version 10 has an old incompatibile version of ESM.
607
+ * Versions prior to 10 did not support ES Modules, and version 10 has an old incompatible version of ESM.
808
608
  * This function returns whether Node.JS has ES Module supports that is compatible with Mocha's needs,
809
609
  * which is version >=12.11.
810
610
  *
@@ -845,3 +645,32 @@ exports.cwd = function cwd() {
845
645
  exports.isBrowser = function isBrowser() {
846
646
  return Boolean(process.browser);
847
647
  };
648
+
649
+ /**
650
+ * Lookup file names at the given `path`.
651
+ *
652
+ * @description
653
+ * Filenames are returned in _traversal_ order by the OS/filesystem.
654
+ * **Make no assumption that the names will be sorted in any fashion.**
655
+ *
656
+ * @public
657
+ * @alias module:lib/cli.lookupFiles
658
+ * @param {string} filepath - Base path to start searching from.
659
+ * @param {string[]} [extensions=[]] - File extensions to look for.
660
+ * @param {boolean} [recursive=false] - Whether to recurse into subdirectories.
661
+ * @return {string[]} An array of paths.
662
+ * @throws {Error} if no files match pattern.
663
+ * @throws {TypeError} if `filepath` is directory and `extensions` not provided.
664
+ * @deprecated Moved to {@link module:lib/cli.lookupFiles}
665
+ */
666
+ exports.lookupFiles = (...args) => {
667
+ if (exports.isBrowser()) {
668
+ throw require('./errors').createUnsupportedError(
669
+ 'lookupFiles() is only supported in Node.js!'
670
+ );
671
+ }
672
+ exports.deprecate(
673
+ '`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
+ return require('./cli').lookupFiles(...args);
676
+ };