Skip to content

armgit / mocha

Public Imported from GitHub
0

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

Make sure `mocha -h` consistently shows help (#2746)

Guangcong Luoda901da1af3d
diff --git a/bin/options.js b/bin/options.js
index be5a60c9..7ee0eca8 100644
--- a/bin/options.js
+++ b/bin/options.js
@@ -17,6 +17,10 @@ module.exports = getOptions;
*/
function getOptions () {
+ if (process.argv.length === 3 && (process.argv[2] === '-h' || process.argv[2] === '--help')) {
+ return;
+ }
+
var optsPath = process.argv.indexOf('--opts') === -1
? 'test/mocha.opts'
: process.argv[process.argv.indexOf('--opts') + 1];
diff --git a/test/integration/fixtures/options/help/test/mocha.opts b/test/integration/fixtures/options/help/test/mocha.opts
new file mode 100644
index 00000000..257cc564
--- /dev/null
+++ b/test/integration/fixtures/options/help/test/mocha.opts
@@ -0,0 +1 @@
+foo
diff --git a/test/integration/helpers.js b/test/integration/helpers.js
index 4f10921a..ec501169 100644
--- a/test/integration/helpers.js
+++ b/test/integration/helpers.js
@@ -110,15 +110,34 @@ module.exports = {
/**
* regular expression used for splitting lines based on new line / dot symbol.
*/
- splitRegExp: new RegExp('[\\n' + baseReporter.symbols.dot + ']+')
+ splitRegExp: new RegExp('[\\n' + baseReporter.symbols.dot + ']+'),
+
+ /**
+ * Invokes the mocha binary. Accepts an array of additional command line args
+ * to pass. The callback is invoked with the exit code and output. Optional
+ * current working directory as final parameter.
+ *
+ * In most cases runMocha should be used instead.
+ *
+ * Example response:
+ * {
+ * code: 1,
+ * output: '...'
+ * }
+ *
+ * @param {Array<string>} args - Extra args to mocha executable
+ * @param {Function} done - Callback
+ * @param {string} cwd - Current working directory for mocha run, optional
+ */
+ invokeMocha: invokeMocha
};
-function invokeMocha (args, fn) {
+function invokeMocha (args, fn, cwd) {
var output, mocha, listener;
output = '';
- args = [path.join('bin', 'mocha')].concat(args);
- mocha = spawn(process.execPath, args);
+ args = [path.join(__dirname, '..', '..', 'bin', 'mocha')].concat(args);
+ mocha = spawn(process.execPath, args, { cwd: cwd });
listener = function (data) {
output += data;
diff --git a/test/integration/options.spec.js b/test/integration/options.spec.js
index 15a46ebf..f77da38b 100644
--- a/test/integration/options.spec.js
+++ b/test/integration/options.spec.js
@@ -1,7 +1,9 @@
'use strict';
+var path = require('path');
var assert = require('assert');
var run = require('./helpers').runMochaJSON;
+var directInvoke = require('./helpers').invokeMocha;
var args = [];
describe('options', function () {
@@ -386,4 +388,14 @@ describe('options', function () {
it('should not force exit after root suite completion', runExit(false, 'disabled'));
});
});
+
+ describe('--help', function () {
+ it('works despite the presence of mocha.opts', function (done) {
+ directInvoke(['-h'], function (error, result) {
+ if (error) { return done(error); }
+ expect(result.output).to.contain('Usage:');
+ done();
+ }, path.join(__dirname, 'fixtures', 'options', 'help'));
+ });
+ });
});