Repository: mochajs/mocha

Problem statement:
6.2.1 fails for node 8.x if there is a --require
<!--
Have you read Mocha's Code of Conduct? By filing an Issue, you are expected to comply with it, including treating everyone with respect: https://github.com/mochajs/mocha/blob/master/.github/CODE_OF_CONDUCT.md
For more, check out the Mocha Gitter chat room: https://gitter.im/mochajs/mocha

Detail the steps necessary to reproduce the problem. To get the fastest support, create an MCVE and upload it to GitHub.
create an [MCVE](https://stackoverflow.com/help/mcve) and upload it to GitHub.
-->

### Prerequisites

<!--
Place an `x` between the square brackets on the lines below for every satisfied prerequisite.
-->

- [x] Checked that your issue hasn't already been filed by cross-referencing [issues with the `faq` label](https://github.com/mochajs/mocha/issues?utf8=%E2%9C%93&q=is%3Aissue%20label%3Afaq%20)
- [x] Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn't just a feature that actually isn't supported in the environment in question or a bug in your code.
- [x] 'Smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
- [x] Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with: `node node_modules/.bin/mocha --version`(Local) and `mocha --version`(Global). We recommend that you _not_ install Mocha globally.

### Description

6.2.0 the command below works

`NODE_ENV=test PORT=9999 node_modules/.bin/mocha -t 5000 --require esm --exit -b --recursive server/test/bootstrap.test.js`

with 6.2.1 if fails with this output on Node 8.x (NB: works on Node 10.x for both)

`bad option: --require=esm`

Node 8.x expects the require to be in this format `--require esm` i.e. no equals `=` sign

I can workaround this by running the underlying node more directly but wanted to let you know about this small regression.

### Steps to Reproduce

See description

**Expected behavior:** [What you expect to happen]

See description

**Actual behavior:** [What actually happens]
<!--
Please include any output, especially error messages (including stacktrace). Remember, we can't see your screen.
Scrub if needed so as not to reveal passwords, etc.
-->

See description

**Reproduces how often:** 100%

### Versions

<!-- If applicable, please specify: -->

- The output of `mocha --version` and `node node_modules/.bin/mocha --version`:
No global install and local is 6.2.1

- The output of `node --version`: v8.16.1

- Your operating system
  - name and version: Mac OS Mojave 10.14.6 also fails on CircleCI node 8 image which I think is Alpine 
  - architecture (32 or 64-bit): 64-bit
- Your shell (e.g., bash, zsh, PowerShell, cmd): zsh
- Your browser and version (if running browser tests): N/A
- Any third-party Mocha-related modules (and their versions): None
- Any code transpiler (e.g., TypeScript, CoffeeScript, Babel) being used (and its version): none / ESM as a loader

### Additional Information

<!--
Any additional information, configuration or data that might be necessary to reproduce the issue.
-->



Hints:
I just hit this; will fix.

probably 5f1cad5ee254ab3ac48d4585726a884255a23583
no, it's not directly responsible, but exposed the bug

Patch:
diff --git a/lib/cli/node-flags.js b/lib/cli/node-flags.js
--- a/lib/cli/node-flags.js
+++ b/lib/cli/node-flags.js
@@ -68,6 +68,7 @@ exports.impliesNoTimeouts = flag => debugFlags.has(flag);
 /**
  * All non-strictly-boolean arguments to node--those with values--must specify those values using `=`, e.g., `--inspect=0.0.0.0`.
  * Unparse these arguments using `yargs-unparser` (which would result in `--inspect 0.0.0.0`), then supply `=` where we have values.
+ * Apparently --require in Node.js v8 does NOT want `=`.
  * There's probably an easier or more robust way to do this; fixes welcome
  * @param {Object} opts - Arguments object
  * @returns {string[]} Unparsed arguments using `=` to specify values
@@ -79,7 +80,9 @@ exports.unparseNodeFlags = opts => {
     ? args
         .join(' ')
         .split(/\b/)
-        .map(arg => (arg === ' ' ? '=' : arg))
+        .map((arg, index, args) =>
+          arg === ' ' && args[index - 1] !== 'require' ? '=' : arg
+        )
         .join('')
         .split(' ')
     : [];


Test patch:
diff --git a/test/node-unit/cli/node-flags.spec.js b/test/node-unit/cli/node-flags.spec.js
--- a/test/node-unit/cli/node-flags.spec.js
+++ b/test/node-unit/cli/node-flags.spec.js
@@ -132,5 +132,14 @@ describe('node-flags', function() {
         ['--v8-numeric-one=1', '--v8-boolean-one', '--v8-numeric-two=2']
       );
     });
+
+    it('should special-case "--require"', function() {
+      // note the only way for this to happen IN REAL LIFE is if you use "--require esm";
+      // mocha eats all --require args otherwise.
+      expect(unparseNodeFlags({require: 'mcrib'}), 'to equal', [
+        '--require',
+        'mcrib'
+      ]);
+    });
   });
 });