mocha 9.1.4 → 9.2.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/cli/config.js CHANGED
@@ -30,10 +30,6 @@ exports.CONFIG_FILES = [
30
30
  '.mocharc.json'
31
31
  ];
32
32
 
33
- const isModuleNotFoundError = err =>
34
- err.code === 'MODULE_NOT_FOUND' ||
35
- err.message.indexOf('Cannot find module') !== -1;
36
-
37
33
  /**
38
34
  * Parsers for various config filetypes. Each accepts a filepath and
39
35
  * returns an object (but could throw)
@@ -41,17 +37,16 @@ const isModuleNotFoundError = err =>
41
37
  const parsers = (exports.parsers = {
42
38
  yaml: filepath => require('js-yaml').load(fs.readFileSync(filepath, 'utf8')),
43
39
  js: filepath => {
44
- const cwdFilepath = path.resolve(filepath);
40
+ let cwdFilepath;
45
41
  try {
46
- debug('parsers: load using cwd-relative path: "%s"', cwdFilepath);
42
+ debug('parsers: load cwd-relative path: "%s"', path.resolve(filepath));
43
+ cwdFilepath = require.resolve(path.resolve(filepath)); // evtl. throws
47
44
  return require(cwdFilepath);
48
45
  } catch (err) {
49
- if (isModuleNotFoundError(err)) {
50
- debug('parsers: retry load as module-relative path: "%s"', filepath);
51
- return require(filepath);
52
- } else {
53
- throw err; // rethrow
54
- }
46
+ if (cwdFilepath) throw err;
47
+
48
+ debug('parsers: retry load as module-relative path: "%s"', filepath);
49
+ return require(filepath);
55
50
  }
56
51
  },
57
52
  json: filepath =>
@@ -225,18 +225,18 @@ exports.validateLegacyPlugin = (opts, pluginType, map = {}) => {
225
225
 
226
226
  // if this exists, then it's already loaded, so nothing more to do.
227
227
  if (!map[pluginId]) {
228
+ let foundId;
228
229
  try {
229
- map[pluginId] = require(pluginId);
230
+ foundId = require.resolve(pluginId);
231
+ map[pluginId] = require(foundId);
230
232
  } catch (err) {
231
- if (err.code === 'MODULE_NOT_FOUND') {
232
- // Try to load reporters from a path (absolute or relative)
233
- try {
234
- map[pluginId] = require(path.resolve(pluginId));
235
- } catch (err) {
236
- throw createUnknownError(err);
237
- }
238
- } else {
239
- throw createUnknownError(err);
233
+ if (foundId) throw createUnknownError(err);
234
+
235
+ // Try to load reporters from a cwd-relative path
236
+ try {
237
+ map[pluginId] = require(path.resolve(pluginId));
238
+ } catch (e) {
239
+ throw createUnknownError(e);
240
240
  }
241
241
  }
242
242
  }
package/lib/mocha.js CHANGED
@@ -16,7 +16,6 @@ var Suite = require('./suite');
16
16
  var esmUtils = require('./nodejs/esm-utils');
17
17
  var createStatsCollector = require('./stats-collector');
18
18
  const {
19
- warn,
20
19
  createInvalidReporterError,
21
20
  createInvalidInterfaceError,
22
21
  createMochaInstanceAlreadyDisposedError,
@@ -335,35 +334,26 @@ Mocha.prototype.reporter = function (reporterName, reporterOptions) {
335
334
  }
336
335
  // Try to load reporters from process.cwd() and node_modules
337
336
  if (!reporter) {
337
+ let foundReporter;
338
338
  try {
339
- reporter = require(reporterName);
339
+ foundReporter = require.resolve(reporterName);
340
+ reporter = require(foundReporter);
340
341
  } catch (err) {
341
- if (err.code === 'MODULE_NOT_FOUND') {
342
- // Try to load reporters from a path (absolute or relative)
343
- try {
344
- reporter = require(path.resolve(utils.cwd(), reporterName));
345
- } catch (_err) {
346
- _err.code === 'MODULE_NOT_FOUND'
347
- ? warn(`'${reporterName}' reporter not found`)
348
- : warn(
349
- `'${reporterName}' reporter blew up with error:\n ${err.stack}`
350
- );
351
- }
352
- } else {
353
- warn(`'${reporterName}' reporter blew up with error:\n ${err.stack}`);
342
+ if (foundReporter) {
343
+ throw createInvalidReporterError(err.message, foundReporter);
344
+ }
345
+ // Try to load reporters from a cwd-relative path
346
+ try {
347
+ reporter = require(path.resolve(reporterName));
348
+ } catch (e) {
349
+ throw createInvalidReporterError(e.message, reporterName);
354
350
  }
355
351
  }
356
352
  }
357
- if (!reporter) {
358
- throw createInvalidReporterError(
359
- `invalid reporter '${reporterName}'`,
360
- reporterName
361
- );
362
- }
363
353
  this._reporter = reporter;
364
354
  }
365
355
  this.options.reporterOption = reporterOptions;
366
- // alias option name is used in public reporters xunit/tap/progress
356
+ // alias option name is used in built-in reporters xunit/tap/progress
367
357
  this.options.reporterOptions = reporterOptions;
368
358
  return this;
369
359
  };
@@ -75,7 +75,23 @@ class BufferedWorkerPool {
75
75
  process.execArgv.join(' ')
76
76
  );
77
77
 
78
- this.options = {...WORKER_POOL_DEFAULT_OPTS, opts, maxWorkers};
78
+ let counter = 0;
79
+ const onCreateWorker = ({forkOpts}) => {
80
+ return {
81
+ forkOpts: {
82
+ ...forkOpts,
83
+ // adds an incremental id to all workers, which can be useful to allocate resources for each process
84
+ env: {...process.env, MOCHA_WORKER_ID: counter++}
85
+ }
86
+ };
87
+ };
88
+
89
+ this.options = {
90
+ ...WORKER_POOL_DEFAULT_OPTS,
91
+ ...opts,
92
+ maxWorkers,
93
+ onCreateWorker
94
+ };
79
95
  this._pool = workerpool.pool(WORKER_PATH, this.options);
80
96
  }
81
97
 
@@ -56,6 +56,11 @@ exports.useColors =
56
56
 
57
57
  exports.inlineDiffs = false;
58
58
 
59
+ /**
60
+ * Truncate diffs longer than this value to avoid slow performance
61
+ */
62
+ exports.maxDiffSize = 8192;
63
+
59
64
  /**
60
65
  * Default color map.
61
66
  */
@@ -188,18 +193,23 @@ function stringifyDiffObjs(err) {
188
193
  * @param {string} expected
189
194
  * @return {string} Diff
190
195
  */
196
+
191
197
  var generateDiff = (exports.generateDiff = function (actual, expected) {
192
198
  try {
193
- const diffSize = 2048;
194
- if (actual.length > diffSize) {
195
- actual = actual.substring(0, diffSize) + ' ... Lines skipped';
199
+ var maxLen = exports.maxDiffSize;
200
+ var skipped = 0;
201
+ if (maxLen > 0) {
202
+ skipped = Math.max(actual.length - maxLen, expected.length - maxLen);
203
+ actual = actual.slice(0, maxLen);
204
+ expected = expected.slice(0, maxLen);
196
205
  }
197
- if (expected.length > diffSize) {
198
- expected = expected.substring(0, diffSize) + ' ... Lines skipped';
199
- }
200
- return exports.inlineDiffs
206
+ let result = exports.inlineDiffs
201
207
  ? inlineDiff(actual, expected)
202
208
  : unifiedDiff(actual, expected);
209
+ if (skipped > 0) {
210
+ result = `${result}\n [mocha] output truncated to ${maxLen} characters, see "maxDiffSize" reporter-option\n`;
211
+ }
212
+ return result;
203
213
  } catch (err) {
204
214
  var msg =
205
215
  '\n ' +
@@ -318,6 +328,12 @@ function Base(runner, options) {
318
328
  this.runner = runner;
319
329
  this.stats = runner.stats; // assigned so Reporters keep a closer reference
320
330
 
331
+ var maxDiffSizeOpt =
332
+ this.options.reporterOption && this.options.reporterOption.maxDiffSize;
333
+ if (maxDiffSizeOpt !== undefined && !isNaN(Number(maxDiffSizeOpt))) {
334
+ exports.maxDiffSize = Number(maxDiffSizeOpt);
335
+ }
336
+
321
337
  runner.on(EVENT_TEST_PASS, function (test) {
322
338
  if (test.duration > test.slow()) {
323
339
  test.speed = 'slow';
package/lib/runner.js CHANGED
@@ -655,7 +655,7 @@ Runner.prototype.parents = function () {
655
655
  * @private
656
656
  */
657
657
  Runner.prototype.runTest = function (fn) {
658
- if (this._opts.dryRun) return fn();
658
+ if (this._opts.dryRun) return Runner.immediately(fn);
659
659
 
660
660
  var self = this;
661
661
  var test = this.test;
package/mocha-es2018.js CHANGED
@@ -1,4 +1,4 @@
1
- // mocha@9.1.4 in javascript ES2018
1
+ // mocha@9.2.2 in javascript ES2018
2
2
  (function (global, factory) {
3
3
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
4
4
  typeof define === 'function' && define.amd ? define(factory) :
@@ -9425,30 +9425,22 @@
9425
9425
 
9426
9426
  var browser$2 = true;
9427
9427
 
9428
- // This alphabet uses `A-Za-z0-9_-` symbols. The genetic algorithm helped
9429
- // optimize the gzip compression for this alphabet.
9430
9428
  let urlAlphabet =
9431
- 'ModuleSymbhasOwnPr-0123456789ABCDEFGHNRVfgctiUvz_KqYTJkLxpZXIjQW';
9432
-
9433
- let customAlphabet = (alphabet, size) => {
9434
- return () => {
9429
+ 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict';
9430
+ let customAlphabet = (alphabet, defaultSize = 21) => {
9431
+ return (size = defaultSize) => {
9435
9432
  let id = '';
9436
- // A compact alternative for `for (var i = 0; i < step; i++)`.
9437
9433
  let i = size;
9438
9434
  while (i--) {
9439
- // `| 0` is more compact and faster than `Math.floor()`.
9440
9435
  id += alphabet[(Math.random() * alphabet.length) | 0];
9441
9436
  }
9442
9437
  return id
9443
9438
  }
9444
9439
  };
9445
-
9446
9440
  let nanoid = (size = 21) => {
9447
9441
  let id = '';
9448
- // A compact alternative for `for (var i = 0; i < step; i++)`.
9449
9442
  let i = size;
9450
9443
  while (i--) {
9451
- // `| 0` is more compact and faster than `Math.floor()`.
9452
9444
  id += urlAlphabet[(Math.random() * 64) | 0];
9453
9445
  }
9454
9446
  return id
@@ -10668,7 +10660,7 @@
10668
10660
 
10669
10661
  /**
10670
10662
  * Selects a color for a debug namespace
10671
- * @param {String} namespace The namespace string for the for the debug instance to be colored
10663
+ * @param {String} namespace The namespace string for the debug instance to be colored
10672
10664
  * @return {Number|String} An ANSI color code for the given namespace
10673
10665
  * @api private
10674
10666
  */
@@ -13617,7 +13609,7 @@
13617
13609
  * @private
13618
13610
  */
13619
13611
  Runner.prototype.runTest = function (fn) {
13620
- if (this._opts.dryRun) return fn();
13612
+ if (this._opts.dryRun) return Runner.immediately(fn);
13621
13613
 
13622
13614
  var self = this;
13623
13615
  var test = this.test;
@@ -14287,6 +14279,11 @@
14287
14279
 
14288
14280
  exports.inlineDiffs = false;
14289
14281
 
14282
+ /**
14283
+ * Truncate diffs longer than this value to avoid slow performance
14284
+ */
14285
+ exports.maxDiffSize = 8192;
14286
+
14290
14287
  /**
14291
14288
  * Default color map.
14292
14289
  */
@@ -14419,18 +14416,23 @@
14419
14416
  * @param {string} expected
14420
14417
  * @return {string} Diff
14421
14418
  */
14419
+
14422
14420
  var generateDiff = (exports.generateDiff = function (actual, expected) {
14423
14421
  try {
14424
- const diffSize = 2048;
14425
- if (actual.length > diffSize) {
14426
- actual = actual.substring(0, diffSize) + ' ... Lines skipped';
14427
- }
14428
- if (expected.length > diffSize) {
14429
- expected = expected.substring(0, diffSize) + ' ... Lines skipped';
14430
- }
14431
- return exports.inlineDiffs
14422
+ var maxLen = exports.maxDiffSize;
14423
+ var skipped = 0;
14424
+ if (maxLen > 0) {
14425
+ skipped = Math.max(actual.length - maxLen, expected.length - maxLen);
14426
+ actual = actual.slice(0, maxLen);
14427
+ expected = expected.slice(0, maxLen);
14428
+ }
14429
+ let result = exports.inlineDiffs
14432
14430
  ? inlineDiff(actual, expected)
14433
14431
  : unifiedDiff(actual, expected);
14432
+ if (skipped > 0) {
14433
+ result = `${result}\n [mocha] output truncated to ${maxLen} characters, see "maxDiffSize" reporter-option\n`;
14434
+ }
14435
+ return result;
14434
14436
  } catch (err) {
14435
14437
  var msg =
14436
14438
  '\n ' +
@@ -14549,6 +14551,12 @@
14549
14551
  this.runner = runner;
14550
14552
  this.stats = runner.stats; // assigned so Reporters keep a closer reference
14551
14553
 
14554
+ var maxDiffSizeOpt =
14555
+ this.options.reporterOption && this.options.reporterOption.maxDiffSize;
14556
+ if (maxDiffSizeOpt !== undefined && !isNaN(Number(maxDiffSizeOpt))) {
14557
+ exports.maxDiffSize = Number(maxDiffSizeOpt);
14558
+ }
14559
+
14552
14560
  runner.on(EVENT_TEST_PASS, function (test) {
14553
14561
  if (test.duration > test.slow()) {
14554
14562
  test.speed = 'slow';
@@ -16492,9 +16500,14 @@
16492
16500
 
16493
16501
  if (options && options.reporterOptions) {
16494
16502
  if (options.reporterOptions.output) {
16495
- {
16503
+ if (!fs.createWriteStream) {
16496
16504
  throw createUnsupportedError('file output not supported in browser');
16497
16505
  }
16506
+
16507
+ fs.mkdirSync(path.dirname(options.reporterOptions.output), {
16508
+ recursive: true
16509
+ });
16510
+ self.fileStream = fs.createWriteStream(options.reporterOptions.output);
16498
16511
  }
16499
16512
 
16500
16513
  // get the suite name from the reporter options (if provided)
@@ -17101,7 +17114,7 @@
17101
17114
  });
17102
17115
 
17103
17116
  var name = "mocha";
17104
- var version = "9.1.4";
17117
+ var version = "9.2.2";
17105
17118
  var homepage = "https://mochajs.org/";
17106
17119
  var notifyLogo = "https://ibin.co/4QuRuGjXvl36.png";
17107
17120
  var _package = {
@@ -18205,7 +18218,6 @@
18205
18218
 
18206
18219
 
18207
18220
  const {
18208
- warn,
18209
18221
  createInvalidReporterError,
18210
18222
  createInvalidInterfaceError,
18211
18223
  createMochaInstanceAlreadyDisposedError,
@@ -18524,35 +18536,26 @@
18524
18536
  }
18525
18537
  // Try to load reporters from process.cwd() and node_modules
18526
18538
  if (!reporter) {
18539
+ let foundReporter;
18527
18540
  try {
18528
- reporter = commonjsRequire(reporterName);
18541
+ foundReporter = require.resolve(reporterName);
18542
+ reporter = commonjsRequire(foundReporter);
18529
18543
  } catch (err) {
18530
- if (err.code === 'MODULE_NOT_FOUND') {
18531
- // Try to load reporters from a path (absolute or relative)
18532
- try {
18533
- reporter = commonjsRequire(path.resolve(utils.cwd(), reporterName));
18534
- } catch (_err) {
18535
- _err.code === 'MODULE_NOT_FOUND'
18536
- ? warn(`'${reporterName}' reporter not found`)
18537
- : warn(
18538
- `'${reporterName}' reporter blew up with error:\n ${err.stack}`
18539
- );
18540
- }
18541
- } else {
18542
- warn(`'${reporterName}' reporter blew up with error:\n ${err.stack}`);
18544
+ if (foundReporter) {
18545
+ throw createInvalidReporterError(err.message, foundReporter);
18546
+ }
18547
+ // Try to load reporters from a cwd-relative path
18548
+ try {
18549
+ reporter = commonjsRequire(path.resolve(reporterName));
18550
+ } catch (e) {
18551
+ throw createInvalidReporterError(e.message, reporterName);
18543
18552
  }
18544
18553
  }
18545
18554
  }
18546
- if (!reporter) {
18547
- throw createInvalidReporterError(
18548
- `invalid reporter '${reporterName}'`,
18549
- reporterName
18550
- );
18551
- }
18552
18555
  this._reporter = reporter;
18553
18556
  }
18554
18557
  this.options.reporterOption = reporterOptions;
18555
- // alias option name is used in public reporters xunit/tap/progress
18558
+ // alias option name is used in built-in reporters xunit/tap/progress
18556
18559
  this.options.reporterOptions = reporterOptions;
18557
18560
  return this;
18558
18561
  };