CodeceptJS icon indicating copy to clipboard operation
CodeceptJS copied to clipboard

Codeceptjs doesn't support to use minimist to pass environment variable

Open YSuMinto opened this issue 3 years ago • 4 comments

What are you trying to achieve?

Using minimist to use -e to passing environment variable. For example adding the following script in package.json

"ci_xxx": "codeceptjs run -c codecept.ci.conf.js --grep @test -e prod",
"stage_xxx": "codeceptjs run -c codecept.ci.conf.js --grep @test -e staging",
"local_xxx": "codeceptjs run -c codecept.ci.conf.js --grep @test -e dev",

What do you get instead?

an error:

unknown option `-e'

Details

It looks like all the supported args are already pre-defined here: https://github.com/codeceptjs/CodeceptJS/blob/3.x/bin/codecept.js

Can we update codeceptjs to support to use minimist? Much Thanks!

YSuMinto avatar Jul 19 '22 22:07 YSuMinto

hey @YSuMinto how about using this https://www.npmjs.com/package/dotenv?

Creating the according .env files like .dev.env, etc. In your codecept.conf.js:

...
switch (process.env.ENV) {
  case 'dev':
    require('dotenv').config({ path: '.dev.env' });
    break;
  case 'prod':
    require('dotenv').config({ path: '.prod.env' });
    break;
  default:
    require('dotenv').config({ path: '.staging.env' });
}
...

following script in package.json

"ci_xxx": "export ENV=prod && codeceptjs run -c codecept.ci.conf.js --grep @test",
"stage_xxx": "export ENV=staging && codeceptjs run -c codecept.ci.conf.js --grep @test",
"local_xxx": "export ENV=dev && codeceptjs run -c codecept.ci.conf.js --grep @test",

Then I believe you'd achieve the same expectations.

kobenguyent avatar Jul 20 '22 06:07 kobenguyent

Thank you @PeterNgTr for the reply!

So far We don't have anything need to be stored in .env file, so it's kind of redundant to add 3 empty files... Also we need to be able to run the automation code under different OS (want to avoid editing code for different OS) and "cross-env" can surely achieve it with "cross-env TEST_ENV=prod".

Just thought minimist can make things a little easier and more flexible...so want to ask whether codeceptjs has the plan to support it.

YSuMinto avatar Jul 20 '22 13:07 YSuMinto

Oh, so @YSuMinto what you wanna pass is only one env var right? Then why not just export ENV=xxx then in your code you can access this environment var like process.env.ENV whenever you need. Not sure if I understand correctly.

kobenguyent avatar Jul 21 '22 04:07 kobenguyent

Hey @PeterNgTr we want to keep the same copy for different OS, export ENV=xxx will not work for Windows we are using cross-env TEST_ENV=prod for all different OS right now, which works fine.

While the request may be a little bit aggressive here... We also want to keep the script/commend shorter. so with minimist it will only need -e prod

Not sure whether it's the intention of codeceptjs on hardcoding the acceptable args, then it kind of results limiting the choices.

YSuMinto avatar Jul 21 '22 14:07 YSuMinto

Yes, we don't support minimist as we use different option to parse arguments and options.

to introduce custom options use environment variables:

TEST_ENV=production codeceptjs run
``

DavertMik avatar Aug 21 '22 20:08 DavertMik