Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Mocha excepting process.argv always contain strings #3861

Closed
4 tasks done
SirAnthony opened this issue Apr 5, 2019 · 2 comments
Closed
4 tasks done

Mocha excepting process.argv always contain strings #3861

SirAnthony opened this issue Apr 5, 2019 · 2 comments
Assignees
Labels
area: node.js command-line-or-Node.js-specific invalid not something we need to work on, such as a non-reproducing issue or an external root cause status: wontfix typically a feature which won't be added, or a "bug" which is actually intended behavior

Comments

@SirAnthony
Copy link

SirAnthony commented Apr 5, 2019

Prerequisites

  • Checked that your issue hasn't already been filed by cross-referencing issues with the faq label
  • 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.
  • '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
  • 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

Running mocha by require it with overriden process.argv will fail if args is not strings. It is hacky way but it allow to use multiple different runners by the same wrapper script, and it worked for mocha2 and mocha4. In mocha 6.0.2 this ends with error.

Steps to Reproduce

const fs = require('fs');
const mocha_file = fs.realpathSync('./node_modules/mocha/bin/_mocha')
process.argv = [process.argv[0], mocha_file, '-t', 20000]
require(mocha_file)

Expected behavior: consume option of number type also

Actual behavior:

current master line

/home/anthony/node_modules/mocha/lib/cli/options.js:87
      const pair = arg.split('=');
                        ^

TypeError: arg.split is not a function
    at nodeArgs.reduce (/home/anthony/node_modules/mocha/lib/cli/options.js:87:25)
    at Array.reduce (<anonymous>)
    at parse (/home/anthony/node_modules/mocha/lib/cli/options.js:85:67)
    at loadOptions (/home/anthony/node_modules/mocha/lib/cli/options.js:288:14)
    at Object.exports.main (/home/anthony/node_modules/mocha/lib/cli/cli.js:62:18)
    at Object.<anonymous> (/home/anthony/node_modules/mocha/bin/_mocha:10:23)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (/home/anthony/test_mocha.js:5:1)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

But fixing it in-place (arg.toString().split('=')) will not work because of yargs-parser similar issue.

Reproduces how often: always

Versions

  • The output of mocha --version and node node_modules/.bin/mocha --version: 6.0.2
  • The output of node --version: v.10.15.3

Additional Information

@plroebuck
Copy link
Contributor

const fs = require('fs');
const mocha_bin = fs.realpathSync('./node_modules/mocha/bin/_mocha');
argv = [process.argv[0], mocha_file, '-t', 20000];
process.argv = argv.map(arg => JSON.stringify(arg));
require(mocha_bin)

@plroebuck plroebuck added invalid not something we need to work on, such as a non-reproducing issue or an external root cause area: node.js command-line-or-Node.js-specific status: wontfix typically a feature which won't be added, or a "bug" which is actually intended behavior and removed unconfirmed-bug labels Apr 6, 2019
@plroebuck plroebuck self-assigned this Apr 6, 2019
@plroebuck
Copy link
Contributor

Node's process.argv has always been a string array.

If your approach worked before, it was by happy accident; it was never supported...
The modification to your script above should fix your problem though.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
area: node.js command-line-or-Node.js-specific invalid not something we need to work on, such as a non-reproducing issue or an external root cause status: wontfix typically a feature which won't be added, or a "bug" which is actually intended behavior
Projects
None yet
Development

No branches or pull requests

2 participants