Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

coerce function for boolean/string/number types

juergbaffe1967c8d2a
diff --git a/lib/cli/options.js b/lib/cli/options.js
index 340fb01e..fcc619a9 100644
--- a/lib/cli/options.js
+++ b/lib/cli/options.js
@@ -54,15 +54,26 @@ const configuration = Object.assign({}, YARGS_PARSER_CONFIG, {
});
/**
- * This is a really fancy way to ensure unique values for `array`-type
- * options.
+ * This is a really fancy way to:
+ * - ensure unique values for `array`-type options
+ * - use its array's last element for `boolean`/`number`/`string`- options given multiple times
* This is passed as the `coerce` option to `yargs-parser`
* @private
* @ignore
*/
-const coerceOpts = types.array.reduce(
- (acc, arg) => Object.assign(acc, {[arg]: v => Array.from(new Set(list(v)))}),
- {}
+const coerceOpts = Object.assign(
+ types.array.reduce(
+ (acc, arg) =>
+ Object.assign(acc, {[arg]: v => Array.from(new Set(list(v)))}),
+ {}
+ ),
+ types.boolean
+ .concat(types.string, types.number)
+ .reduce(
+ (acc, arg) =>
+ Object.assign(acc, {[arg]: v => (Array.isArray(v) ? v.pop() : v)}),
+ {}
+ )
);
/**
diff --git a/lib/cli/run-option-metadata.js b/lib/cli/run-option-metadata.js
index f19bd577..fbc4ea90 100644
--- a/lib/cli/run-option-metadata.js
+++ b/lib/cli/run-option-metadata.js
@@ -45,7 +45,17 @@ exports.types = {
'watch'
],
number: ['retries'],
- string: ['fgrep', 'grep', 'package', 'reporter', 'ui', 'slow', 'timeout']
+ string: [
+ 'config',
+ 'fgrep',
+ 'grep',
+ 'opts',
+ 'package',
+ 'reporter',
+ 'ui',
+ 'slow',
+ 'timeout'
+ ]
};
/**
diff --git a/lib/cli/run.js b/lib/cli/run.js
index 0541ec6a..da5ffd7b 100644
--- a/lib/cli/run.js
+++ b/lib/cli/run.js
@@ -258,13 +258,6 @@ exports.builder = yargs =>
}
});
- types.boolean
- .concat(types.string, types.number)
- .filter(opt => Array.isArray(argv[opt]))
- .forEach(opt => {
- argv[opt] = argv[opt].pop();
- });
-
// yargs.implies() isn't flexible enough to handle this
if (argv.invert && !('fgrep' in argv || 'grep' in argv)) {
throw createMissingArgumentError(
diff --git a/test/node-unit/cli/options.spec.js b/test/node-unit/cli/options.spec.js
index 2e1e4aa0..3c39cc7f 100644
--- a/test/node-unit/cli/options.spec.js
+++ b/test/node-unit/cli/options.spec.js
@@ -86,7 +86,7 @@ describe('options', function() {
config: false,
opts: false,
package: false,
- retries: 3
+ retries: '3'
})
);
});
@@ -202,7 +202,7 @@ describe('options', function() {
config: false,
opts: false,
package: false,
- retries: 3
+ retries: '3'
}
)
);
@@ -427,7 +427,7 @@ describe('options', function() {
config: false,
opts: false,
package: false,
- retries: 3
+ retries: '3'
})
);
});
@@ -476,7 +476,7 @@ describe('options', function() {
config: false,
opts: false,
package: false,
- retries: 3
+ retries: '3'
})
);
});