mocha 7.2.0 → 8.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.
Files changed (46) hide show
  1. package/CHANGELOG.md +116 -0
  2. package/bin/mocha +17 -2
  3. package/browser-entry.js +26 -9
  4. package/lib/browser/growl.js +2 -1
  5. package/lib/browser/highlight-tags.js +39 -0
  6. package/lib/browser/parse-query.js +24 -0
  7. package/lib/browser/progress.js +4 -0
  8. package/lib/browser/template.html +7 -5
  9. package/lib/cli/cli.js +2 -2
  10. package/lib/cli/collect-files.js +15 -9
  11. package/lib/cli/config.js +0 -1
  12. package/lib/cli/init.js +1 -2
  13. package/lib/cli/lookup-files.js +145 -0
  14. package/lib/cli/node-flags.js +2 -2
  15. package/lib/cli/options.js +11 -87
  16. package/lib/cli/run-helpers.js +54 -16
  17. package/lib/cli/run-option-metadata.js +4 -2
  18. package/lib/cli/run.js +61 -14
  19. package/lib/cli/watch-run.js +211 -51
  20. package/lib/context.js +0 -15
  21. package/lib/errors.js +26 -3
  22. package/lib/esm-utils.js +11 -6
  23. package/lib/hook.js +24 -0
  24. package/lib/interfaces/common.js +19 -11
  25. package/lib/mocha.js +137 -121
  26. package/lib/mocharc.json +0 -1
  27. package/lib/nodejs/buffered-worker-pool.js +174 -0
  28. package/lib/{growl.js → nodejs/growl.js} +3 -2
  29. package/lib/nodejs/parallel-buffered-runner.js +295 -0
  30. package/lib/nodejs/reporters/parallel-buffered.js +133 -0
  31. package/lib/nodejs/serializer.js +404 -0
  32. package/lib/nodejs/worker.js +154 -0
  33. package/lib/pending.js +4 -0
  34. package/lib/reporters/base.js +25 -12
  35. package/lib/reporters/landing.js +3 -3
  36. package/lib/reporters/tap.js +1 -2
  37. package/lib/reporters/xunit.js +3 -2
  38. package/lib/runnable.js +18 -30
  39. package/lib/runner.js +58 -64
  40. package/lib/suite.js +32 -24
  41. package/lib/test.js +28 -1
  42. package/lib/utils.js +19 -206
  43. package/mocha.js +25522 -18248
  44. package/mocha.js.map +1 -0
  45. package/package.json +52 -42
  46. package/lib/browser/tty.js +0 -13
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,141 +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
- * @memberof Mocha.utils
492
- * @param {string} filepath - Base path to start searching from.
493
- * @param {string[]} [extensions=[]] - File extensions to look for.
494
- * @param {boolean} [recursive=false] - Whether to recurse into subdirectories.
495
- * @return {string[]} An array of paths.
496
- * @throws {Error} if no files match pattern.
497
- * @throws {TypeError} if `filepath` is directory and `extensions` not provided.
498
- */
499
- exports.lookupFiles = function lookupFiles(filepath, extensions, recursive) {
500
- extensions = extensions || [];
501
- recursive = recursive || false;
502
- var files = [];
503
- var stat;
504
-
505
- if (!fs.existsSync(filepath)) {
506
- var pattern;
507
- if (glob.hasMagic(filepath)) {
508
- // Handle glob as is without extensions
509
- pattern = filepath;
510
- } else {
511
- // glob pattern e.g. 'filepath+(.js|.ts)'
512
- var strExtensions = extensions
513
- .map(function(v) {
514
- return '.' + v;
515
- })
516
- .join('|');
517
- pattern = filepath + '+(' + strExtensions + ')';
518
- }
519
- files = glob.sync(pattern, {nodir: true});
520
- if (!files.length) {
521
- throw createNoFilesMatchPatternError(
522
- 'Cannot find any files matching pattern ' + exports.dQuote(filepath),
523
- filepath
524
- );
525
- }
526
- return files;
527
- }
528
-
529
- // Handle file
530
- try {
531
- stat = fs.statSync(filepath);
532
- if (stat.isFile()) {
533
- return filepath;
534
- }
535
- } catch (err) {
536
- // ignore error
537
- return;
538
- }
539
-
540
- // Handle directory
541
- fs.readdirSync(filepath).forEach(function(dirent) {
542
- var pathname = path.join(filepath, dirent);
543
- var stat;
544
-
545
- try {
546
- stat = fs.statSync(pathname);
547
- if (stat.isDirectory()) {
548
- if (recursive) {
549
- files = files.concat(lookupFiles(pathname, extensions, recursive));
550
- }
551
- return;
552
- }
553
- } catch (err) {
554
- // ignore error
555
- return;
556
- }
557
- if (!extensions.length) {
558
- throw createMissingArgumentError(
559
- util.format(
560
- 'Argument %s required when argument %s is a directory',
561
- exports.sQuote('extensions'),
562
- exports.sQuote('filepath')
563
- ),
564
- 'extensions',
565
- 'array'
566
- );
567
- }
568
-
569
- if (
570
- !stat.isFile() ||
571
- !hasMatchingExtname(pathname, extensions) ||
572
- isHiddenOnUnix(pathname)
573
- ) {
574
- return;
575
- }
576
- files.push(pathname);
577
- });
578
-
579
- return files;
580
- };
581
-
582
381
  /**
583
382
  * process.emitWarning or a polyfill
584
383
  * @see https://nodejs.org/api/process.html#process_process_emitwarning_warning_options
@@ -805,20 +604,24 @@ exports.defineConstants = function(obj) {
805
604
  * Whether current version of Node support ES modules
806
605
  *
807
606
  * @description
808
- * 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.
809
608
  * This function returns whether Node.JS has ES Module supports that is compatible with Mocha's needs,
810
609
  * which is version >=12.11.
811
610
  *
611
+ * @param {partialSupport} whether the full Node.js ESM support is available (>= 12) or just something that supports the runtime (>= 10)
612
+ *
812
613
  * @returns {Boolean} whether the current version of Node.JS supports ES Modules in a way that is compatible with Mocha
813
614
  */
814
- exports.supportsEsModules = function() {
815
- if (!process.browser && process.versions && process.versions.node) {
615
+ exports.supportsEsModules = function(partialSupport) {
616
+ if (!exports.isBrowser() && process.versions && process.versions.node) {
816
617
  var versionFields = process.versions.node.split('.');
817
618
  var major = +versionFields[0];
818
619
  var minor = +versionFields[1];
819
620
 
820
- if (major >= 13 || (major === 12 && minor >= 11)) {
821
- return true;
621
+ if (!partialSupport) {
622
+ return major >= 13 || (major === 12 && minor >= 11);
623
+ } else {
624
+ return major >= 10;
822
625
  }
823
626
  }
824
627
  };
@@ -832,3 +635,13 @@ exports.supportsEsModules = function() {
832
635
  exports.cwd = function cwd() {
833
636
  return process.cwd();
834
637
  };
638
+
639
+ /**
640
+ * Returns `true` if Mocha is running in a browser.
641
+ * Checks for `process.browser`.
642
+ * @returns {boolean}
643
+ * @private
644
+ */
645
+ exports.isBrowser = function isBrowser() {
646
+ return Boolean(process.browser);
647
+ };