mocha 8.3.2 → 9.0.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.
package/lib/runner.js CHANGED
@@ -4,7 +4,6 @@
4
4
  * Module dependencies.
5
5
  * @private
6
6
  */
7
- var util = require('util');
8
7
  var EventEmitter = require('events').EventEmitter;
9
8
  var Pending = require('./pending');
10
9
  var utils = require('./utils');
@@ -19,8 +18,6 @@ var EVENT_ROOT_SUITE_RUN = Suite.constants.EVENT_ROOT_SUITE_RUN;
19
18
  var STATE_FAILED = Runnable.constants.STATE_FAILED;
20
19
  var STATE_PASSED = Runnable.constants.STATE_PASSED;
21
20
  var STATE_PENDING = Runnable.constants.STATE_PENDING;
22
- var dQuote = utils.dQuote;
23
- var sQuote = utils.sQuote;
24
21
  var stackFilter = utils.stackTraceFilter();
25
22
  var stringify = utils.stringify;
26
23
 
@@ -138,8 +135,9 @@ class Runner extends EventEmitter {
138
135
  * @public
139
136
  * @class
140
137
  * @param {Suite} suite - Root suite
141
- * @param {Object|boolean} [opts] - Options. If `boolean`, whether or not to delay execution of root suite until ready (for backwards compatibility).
138
+ * @param {Object|boolean} [opts] - Options. If `boolean` (deprecated), whether or not to delay execution of root suite until ready.
142
139
  * @param {boolean} [opts.delay] - Whether to delay execution of root suite until ready.
140
+ * @param {boolean} [opts.dryRun] - Whether to report tests without running them.
143
141
  * @param {boolean} [opts.cleanReferencesAfterRun] - Whether to clean references to test fns and hooks when a suite is done.
144
142
  */
145
143
  constructor(suite, opts) {
@@ -148,7 +146,10 @@ class Runner extends EventEmitter {
148
146
  opts = {};
149
147
  }
150
148
  if (typeof opts === 'boolean') {
151
- // TODO: deprecate this
149
+ // TODO: remove this
150
+ require('./errors').deprecate(
151
+ '"Runner(suite: Suite, delay: boolean)" is deprecated. Use "Runner(suite: Suite, {delay: boolean})" instead.'
152
+ );
152
153
  this._delay = opts;
153
154
  opts = {};
154
155
  } else {
@@ -407,9 +408,8 @@ Runner.prototype.checkGlobals = function(test) {
407
408
  this._globals = this._globals.concat(leaks);
408
409
 
409
410
  if (leaks.length) {
410
- var msg = 'global leak(s) detected: %s';
411
- var error = new Error(util.format(msg, leaks.map(sQuote).join(', ')));
412
- this.fail(test, error);
411
+ var msg = `global leak(s) detected: ${leaks.map(e => `'${e}'`).join(', ')}`;
412
+ this.fail(test, new Error(msg));
413
413
  }
414
414
  };
415
415
 
@@ -476,6 +476,8 @@ Runner.prototype.fail = function(test, err, force) {
476
476
  */
477
477
 
478
478
  Runner.prototype.hook = function(name, fn) {
479
+ if (this._opts.dryRun) return fn();
480
+
479
481
  var suite = this.suite;
480
482
  var hooks = suite.getHooks(name);
481
483
  var self = this;
@@ -554,8 +556,7 @@ Runner.prototype.hook = function(name, fn) {
554
556
  function setHookTitle(hook) {
555
557
  hook.originalTitle = hook.originalTitle || hook.title;
556
558
  if (hook.ctx && hook.ctx.currentTest) {
557
- hook.title =
558
- hook.originalTitle + ' for ' + dQuote(hook.ctx.currentTest.title);
559
+ hook.title = `${hook.originalTitle} for "${hook.ctx.currentTest.title}"`;
559
560
  } else {
560
561
  var parentTitle;
561
562
  if (hook.parent.title) {
@@ -563,7 +564,7 @@ Runner.prototype.hook = function(name, fn) {
563
564
  } else {
564
565
  parentTitle = hook.parent.root ? '{root}' : '';
565
566
  }
566
- hook.title = hook.originalTitle + ' in ' + dQuote(parentTitle);
567
+ hook.title = `${hook.originalTitle} in "${parentTitle}"`;
567
568
  }
568
569
  }
569
570
  }
@@ -609,7 +610,7 @@ Runner.prototype.hooks = function(name, suites, fn) {
609
610
  };
610
611
 
611
612
  /**
612
- * Run hooks from the top level down.
613
+ * Run 'afterEach' hooks from bottom up.
613
614
  *
614
615
  * @param {String} name
615
616
  * @param {Function} fn
@@ -621,7 +622,7 @@ Runner.prototype.hookUp = function(name, fn) {
621
622
  };
622
623
 
623
624
  /**
624
- * Run hooks from the bottom up.
625
+ * Run 'beforeEach' hooks from top level down.
625
626
  *
626
627
  * @param {String} name
627
628
  * @param {Function} fn
@@ -656,6 +657,8 @@ Runner.prototype.parents = function() {
656
657
  * @private
657
658
  */
658
659
  Runner.prototype.runTest = function(fn) {
660
+ if (this._opts.dryRun) return fn();
661
+
659
662
  var self = this;
660
663
  var test = this.test;
661
664
 
@@ -701,7 +704,6 @@ Runner.prototype.runTests = function(suite, fn) {
701
704
  self.suite = after ? errSuite.parent : errSuite;
702
705
 
703
706
  if (self.suite) {
704
- // call hookUp afterEach
705
707
  self.hookUp(HOOK_TYPE_AFTER_EACH, function(err2, errSuite2) {
706
708
  self.suite = orig;
707
709
  // some hooks may fail even now
package/lib/utils.js CHANGED
@@ -13,7 +13,6 @@ const {nanoid} = require('nanoid/non-secure');
13
13
  var path = require('path');
14
14
  var util = require('util');
15
15
  var he = require('he');
16
- const errors = require('./errors');
17
16
 
18
17
  const MOCHA_ID_PROP_NAME = '__mocha_id__';
19
18
 
@@ -519,44 +518,6 @@ exports.clamp = function clamp(value, range) {
519
518
  return Math.min(Math.max(value, range[0]), range[1]);
520
519
  };
521
520
 
522
- /**
523
- * Single quote text by combining with undirectional ASCII quotation marks.
524
- *
525
- * @description
526
- * Provides a simple means of markup for quoting text to be used in output.
527
- * Use this to quote names of variables, methods, and packages.
528
- *
529
- * <samp>package 'foo' cannot be found</samp>
530
- *
531
- * @private
532
- * @param {string} str - Value to be quoted.
533
- * @returns {string} quoted value
534
- * @example
535
- * sQuote('n') // => 'n'
536
- */
537
- exports.sQuote = function(str) {
538
- return "'" + str + "'";
539
- };
540
-
541
- /**
542
- * Double quote text by combining with undirectional ASCII quotation marks.
543
- *
544
- * @description
545
- * Provides a simple means of markup for quoting text to be used in output.
546
- * Use this to quote names of datatypes, classes, pathnames, and strings.
547
- *
548
- * <samp>argument 'value' must be "string" or "number"</samp>
549
- *
550
- * @private
551
- * @param {string} str - Value to be quoted.
552
- * @returns {string} quoted value
553
- * @example
554
- * dQuote('number') // => "number"
555
- */
556
- exports.dQuote = function(str) {
557
- return '"' + str + '"';
558
- };
559
-
560
521
  /**
561
522
  * It's a noop.
562
523
  * @public
@@ -650,35 +611,6 @@ exports.isBrowser = function isBrowser() {
650
611
  return Boolean(process.browser);
651
612
  };
652
613
 
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
614
  /*
683
615
  * Casts `value` to an array; useful for optionally accepting array parameters
684
616
  *