mocha 11.0.1 → 11.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/cli/cli.js CHANGED
@@ -42,7 +42,11 @@ exports.main = (argv = process.argv.slice(2), mochaArgs) => {
42
42
  module.paths.push(cwd(), path.resolve('node_modules'));
43
43
  }
44
44
 
45
- Error.stackTraceLimit = Infinity; // configurable via --stack-trace-limit?
45
+ try {
46
+ Error.stackTraceLimit = Infinity; // configurable via --stack-trace-limit?
47
+ } catch (err) {
48
+ debug('unable to set Error.stackTraceLimit = Infinity', err);
49
+ }
46
50
 
47
51
  var args = mochaArgs || loadOptions(argv);
48
52
 
@@ -10,7 +10,12 @@
10
10
  const fs = require('fs');
11
11
  const ansi = require('ansi-colors');
12
12
  const yargsParser = require('yargs-parser');
13
- const {types, aliases} = require('./run-option-metadata');
13
+ const {
14
+ types,
15
+ aliases,
16
+ isMochaFlag,
17
+ expectedTypeForFlag
18
+ } = require('./run-option-metadata');
14
19
  const {ONE_AND_DONE_ARGS} = require('./one-and-dones');
15
20
  const mocharc = require('../mocharc.json');
16
21
  const {list} = require('./run-helpers');
@@ -18,7 +23,12 @@ const {loadConfig, findConfig} = require('./config');
18
23
  const findUp = require('find-up');
19
24
  const debug = require('debug')('mocha:cli:options');
20
25
  const {isNodeFlag} = require('./node-flags');
21
- const {createUnparsableFileError} = require('../errors');
26
+ const {
27
+ createUnparsableFileError,
28
+ createInvalidArgumentTypeError,
29
+ createUnsupportedError
30
+ } = require('../errors');
31
+ const {isNumeric} = require('../utils');
22
32
 
23
33
  /**
24
34
  * The `yargs-parser` namespace
@@ -93,6 +103,44 @@ const nargOpts = types.array
93
103
  .concat(types.string, types.number)
94
104
  .reduce((acc, arg) => Object.assign(acc, {[arg]: 1}), {});
95
105
 
106
+ /**
107
+ * Throws either "UNSUPPORTED" error or "INVALID_ARG_TYPE" error for numeric positional arguments.
108
+ * @param {string[]} allArgs - Stringified args passed to mocha cli
109
+ * @param {number} numericArg - Numeric positional arg for which error must be thrown
110
+ * @param {Object} parsedResult - Result from `yargs-parser`
111
+ * @private
112
+ * @ignore
113
+ */
114
+ const createErrorForNumericPositionalArg = (
115
+ numericArg,
116
+ allArgs,
117
+ parsedResult
118
+ ) => {
119
+ // A flag for `numericArg` exists if:
120
+ // 1. A mocha flag immediately preceeded the numericArg in `allArgs` array and
121
+ // 2. `numericArg` value could not be assigned to this flag by `yargs-parser` because of incompatible datatype.
122
+ const flag = allArgs.find((arg, index) => {
123
+ const normalizedArg = arg.replace(/^--?/, '');
124
+ return (
125
+ isMochaFlag(arg) &&
126
+ allArgs[index + 1] === String(numericArg) &&
127
+ parsedResult[normalizedArg] !== String(numericArg)
128
+ );
129
+ });
130
+
131
+ if (flag) {
132
+ throw createInvalidArgumentTypeError(
133
+ `Mocha flag '${flag}' given invalid option: '${numericArg}'`,
134
+ numericArg,
135
+ expectedTypeForFlag(flag)
136
+ );
137
+ } else {
138
+ throw createUnsupportedError(
139
+ `Option ${numericArg} is unsupported by the mocha cli`
140
+ );
141
+ }
142
+ };
143
+
96
144
  /**
97
145
  * Wrapper around `yargs-parser` which applies our settings
98
146
  * @param {string|string[]} args - Arguments to parse
@@ -104,24 +152,20 @@ const nargOpts = types.array
104
152
  const parse = (args = [], defaultValues = {}, ...configObjects) => {
105
153
  // save node-specific args for special handling.
106
154
  // 1. when these args have a "=" they should be considered to have values
107
- // 2. if they don't, they just boolean flags
155
+ // 2. if they don't, they are just boolean flags
108
156
  // 3. to avoid explicitly defining the set of them, we tell yargs-parser they
109
157
  // are ALL boolean flags.
110
158
  // 4. we can then reapply the values after yargs-parser is done.
111
- const nodeArgs = (Array.isArray(args) ? args : args.split(' ')).reduce(
112
- (acc, arg) => {
113
- const pair = arg.split('=');
114
- let flag = pair[0];
115
- if (isNodeFlag(flag, false)) {
116
- flag = flag.replace(/^--?/, '');
117
- return arg.includes('=')
118
- ? acc.concat([[flag, pair[1]]])
119
- : acc.concat([[flag, true]]);
120
- }
121
- return acc;
122
- },
123
- []
124
- );
159
+ const allArgs = Array.isArray(args) ? args : args.split(' ');
160
+ const nodeArgs = allArgs.reduce((acc, arg) => {
161
+ const pair = arg.split('=');
162
+ let flag = pair[0];
163
+ if (isNodeFlag(flag, false)) {
164
+ flag = flag.replace(/^--?/, '');
165
+ return acc.concat([[flag, arg.includes('=') ? pair[1] : true]]);
166
+ }
167
+ return acc;
168
+ }, []);
125
169
 
126
170
  const result = yargsParser.detailed(args, {
127
171
  configuration,
@@ -140,6 +184,15 @@ const parse = (args = [], defaultValues = {}, ...configObjects) => {
140
184
  process.exit(1);
141
185
  }
142
186
 
187
+ const numericPositionalArg = result.argv._.find(arg => isNumeric(arg));
188
+ if (numericPositionalArg) {
189
+ createErrorForNumericPositionalArg(
190
+ numericPositionalArg,
191
+ allArgs,
192
+ result.argv
193
+ );
194
+ }
195
+
143
196
  // reapply "=" arg values from above
144
197
  nodeArgs.forEach(([key, value]) => {
145
198
  result.argv[key] = value;
@@ -114,3 +114,24 @@ const ALL_MOCHA_FLAGS = Object.keys(TYPES).reduce((acc, key) => {
114
114
  exports.isMochaFlag = flag => {
115
115
  return ALL_MOCHA_FLAGS.has(flag.replace(/^--?/, ''));
116
116
  };
117
+
118
+ /**
119
+ * Returns expected yarg option type for a given mocha flag.
120
+ * @param {string} flag - Flag to check (can be with or without leading dashes "--"")
121
+ * @returns {string | undefined} - If flag is a valid mocha flag, the expected type of argument for this flag is returned, otherwise undefined is returned.
122
+ * @private
123
+ */
124
+ exports.expectedTypeForFlag = flag => {
125
+ const normalizedName = flag.replace(/^--?/, '');
126
+
127
+ // If flag is an alias, get it's full name.
128
+ const aliases = exports.aliases;
129
+ const fullFlagName =
130
+ Object.keys(aliases).find(flagName =>
131
+ aliases[flagName].includes(normalizedName)
132
+ ) || normalizedName;
133
+
134
+ return Object.keys(TYPES).find(flagType =>
135
+ TYPES[flagType].includes(fullFlagName)
136
+ );
137
+ };
package/lib/utils.js CHANGED
@@ -689,3 +689,10 @@ exports.breakCircularDeps = inputObj => {
689
689
 
690
690
  return _breakCircularDeps(inputObj);
691
691
  };
692
+
693
+ /**
694
+ * Checks if provided input can be parsed as a JavaScript Number.
695
+ */
696
+ exports.isNumeric = input => {
697
+ return !isNaN(parseFloat(input));
698
+ };
package/mocha.js CHANGED
@@ -1,4 +1,4 @@
1
- // mocha@11.0.1 in javascript ES2018
1
+ // mocha@11.0.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) :
@@ -11652,6 +11652,13 @@
11652
11652
 
11653
11653
  return _breakCircularDeps(inputObj);
11654
11654
  };
11655
+
11656
+ /**
11657
+ * Checks if provided input can be parsed as a JavaScript Number.
11658
+ */
11659
+ exports.isNumeric = input => {
11660
+ return !isNaN(parseFloat(input));
11661
+ };
11655
11662
  }(utils$3));
11656
11663
 
11657
11664
  var _nodeResolve_empty = {};
@@ -19201,7 +19208,7 @@
19201
19208
  };
19202
19209
 
19203
19210
  var name = "mocha";
19204
- var version = "11.0.1";
19211
+ var version = "11.0.2";
19205
19212
  var homepage = "https://mochajs.org/";
19206
19213
  var notifyLogo = "https://ibin.co/4QuRuGjXvl36.png";
19207
19214
  var require$$17 = {