mocks-server / main

Node.js mock server running live, interactive mocks in place of real APIs
https://www.mocks-server.org
Apache License 2.0
288 stars 16 forks source link

JavaScript API: createServer command with config object throws error "unknown option '--config'" #488

Closed y7haar closed 9 months ago

y7haar commented 9 months ago

Describe the bug When using the command createServer with a given config object, mocks server always crashes and throws the error:

error: unknown option '--config'
  ●  process.exit called with "1"
      at Command._exit (node_modules/@mocks-server/config/node_modules/commander/lib/command.js:458:13)
      at Command._displayError (node_modules/@mocks-server/config/node_modules/commander/lib/command.js:1466:10)
      at Command.unknownOption (node_modules/@mocks-server/config/node_modules/commander/lib/command.js:1555:10)
      at checkForUnknownOptions (node_modules/@mocks-server/config/node_modules/commander/lib/command.js:1216:14)
      at Command._parseCommand (node_modules/@mocks-server/config/node_modules/commander/lib/command.js:1254:7)
      at Command.parse (node_modules/@mocks-server/config/node_modules/commander/lib/command.js:897:10)
      at CommandLineArguments.read (node_modules/@mocks-server/config/src/CommandLineArguments.js:119:13)
      at Config._loadFromArgs (node_modules/@mocks-server/config/src/Config.js:113:23)
      at Config._load (node_modules/@mocks-server/config/src/Config.js:165:35)
      at Config.load (node_modules/@mocks-server/config/src/Config.js:187:16)
      at Core._loadConfig (node_modules/@mocks-server/core/src/Core.js:154:24)
      at Core.init (node_modules/@mocks-server/core/src/Core.js:199:16)
      at Core.start (node_modules/@mocks-server/core/src/Core.js:209:5)

To Reproduce

const core = createServer({
      config: {
        readArguments: true,
        readEnvironment: true,
        readFile: true,
      },
      plugins: {
        inquirerCli: {
          enabled: true,
        },
      },
      files: {
        enabled: true,
      },
    });

core.start();

Expected behavior Mocks Server should start and load all routes from the mocks directory as described in the docs.

Operating system, Node.js an npm versions, or browser version (please complete the following information):

javierbrea commented 9 months ago

Hi @y7haar , when you enable the mock server to readArguments, it validates by default that all arguments are valid, and it throws an error in case not. So, you are probably passing a --config argument to your command, and that produces the error.

You have some options to avoid this:

You can read more about server configuration here

y7haar commented 9 months ago

Hi @javierbrea, thanks for your quick response! Setting readArguments to false did the trick.

We're using mocks-server together with detox to test our react native app. The initialization of mocks-server happens inside the detox context, maybe this is why the error occurred.