ketanSaxena / schema-validator

Schema validation utility for YAML/JSON files against a pre defined schema
https://ketantechracers.github.io/schema-validator
MIT License
36 stars 17 forks source link

mocha --opts not working anymore #7

Closed thomasweitzel closed 4 years ago

thomasweitzel commented 5 years ago

As soon as I include "yaml-schema-validator": "^1.1.9" in my package.json and npm install it, mocha --opts mocha.opts fails with this error message:

$ mocha --opts mocha.opts
error: unknown option `--opts'

My project has these dependencies:

"devDependencies": {
    "chai": "^4.2.0",
    "chai-as-promised": "^7.1.1",
    "eslint": "^6.5.1",
    "eslint-config-airbnb-base": "^14.0.0",
    "eslint-plugin-import": "^2.14.0",
    "fast-xml-parser": "3.13.0",
    "mocha": "^6.2.1",
    "nyc": "^14.1.1",
    "yaml-schema-validator": "^1.1.9"
  },
  "dependencies": {
    "app-root-path": "^2.2.1",
    "bluebird": "^3.7.0",
    "config": "^3.2.3",
    "glob": "^7.1.4",
    "js-yaml": "^3.13.1",
    "json5": "^2.1.1",
    "jszip": "^3.2.2",
    "lodash": "^4.17.11",
    "moment": "^2.22.2",
    "moment-range": "^4.0.1",
    "moment-timezone": "^0.5.23",
    "normalize-path": "^3.0.0",
    "ramda": "^0.26.1",
    "sql.js": "^1.0.0",
    "winston": "^3.2.1",
    "yargs": "^14.2.0"
  }

If I remove yaml-schema-validator, the command mocha --opts mocha.opts runs all tests as expected.

The problematic behavior is observed regardless of platform, it's the same on Linux, Windows, or macOS. Node.js version 10.16.3 was used.

bkimminich commented 4 years ago

I have observed the exact same behavior with Jest. Having yaml-schema-validator in the package.json and installed does not cause issues for me. Only when it is imported with require('yaml-schema-validator') in my project, the error: unknown option xxxxx errors arise.

See https://github.com/facebook/jest/issues/9379 for my original report assuming Jest would be the culprit. It contains steps to reproduce the issue from the failing commits from my Travis-CI pipeline.

bkimminich commented 4 years ago

I just tried with "yaml-schema-validator": "1.0.0" pinned dependency and the issue exists there already, so it is probably there from the beginning and not a regression.

C:\Data\GitHub\juice-shop>npm run frisby

> juice-shop@10.0.0-SNAPSHOT frisby C:\Data\GitHub\juice-shop
> nyc --report-dir=./build/reports/coverage/api-tests jest --silent --runInBand --forceExit

Determining test suites to run...error: unknown option `--silent'
bkimminich commented 4 years ago

Removing the only const validateSchema = require('yaml-schema-validator') in my code base (which does not even have to be called - it just has to be imported) makes my test suite run perfectly fine again.

bkimminich commented 4 years ago

I think the culprit is the design of the module as a mix of CLI and programmatic library all in one. I have no experience with the commander module, but I suspect that this code section is somehow "eating" the command line arguments of the jest or mocha call:

program
  .version(pkg.version)
  .command('validate')
  .option('-f, --filePath <filePath>', 'path to the target file for validating')
  .option('-j, --json','passed if target file is in JSON format')
  .option('-s, --schema [schemaPath]','path to an external schema.js file')
  .action(validateSchema);

program.parse(process.argv);

if(process.argv.includes('help')) {
  help()
}

If this would be extracted into its own file - which then serves for the CLI use case - the programmatic use case would import the actual validator only but not the CLI code.

bkimminich commented 4 years ago

✅ The problem can be bypassed by programmatically importing const validateSchema = require('yaml-schema-validator/src') as a workaround.

rockon1985 commented 4 years ago

thanks @bkimminich for coming up with this. It can work as a temporary workaround till I mitigate the root cause. Looks like an issue in one of the dependencies and not in the package itself. Would certainly recommend to upgrade to node version 10+ or 11 to avoid this scenario. Closing this issue.

bkimminich commented 4 years ago

I'm using Node 10, 12 and 13 in my project and they all had this problem.

alambike commented 2 years ago

Same error here running jest --coverage. Tested with the following node versions:


 ●  process.exit called with "1"

      5 |
    > 6 | const validateSchema = require('yaml-schema-validator');
        |                        ^
      8 |

      at Command.Object.<anonymous>.Command.unknownOption (node_modules/yaml-schema-validator/node_modules/commander/index.js:823:11)
      at Command.Object.<anonymous>.Command.parseArgs (node_modules/yaml-schema-validator/node_modules/commander/index.js:661:12)
      at Command.Object.<anonymous>.Command.parse (node_modules/yaml-schema-validator/node_modules/commander/index.js:474:21)
      at Object.<anonymous> (node_modules/yaml-schema-validator/index.js:24:9)
      at Object.<anonymous> (src/config/settings.js:6:24)