Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
101 views
in Technique[技术] by (71.8m points)

node.js - How to set Environment Variable and How to pass the environment Variable as a parameter

In package.json file I have some list of task under the scripts

"scripts": {
    "test:smoketest":"cucumber-js --format json:./reports/cucumber-json-report.json --tags '@Smoke_Test'",
    "test:regressiontest":"cucumber-js --format json:./reports/cucumber-json-report.json --tags '@Regression_Test'",
    "test:UI":"cucumber-js --format json:./reports/cucumber-json-report.json --tags '@UI'"
  },

I will be executing the script with the command Eg: npm run test:smoketest(npm run test:testname)I want to pass this test name in a function to check for a if condition like

test.js

async function(){
if(testname=="UI"){
some tasks need to be done
}
else{
some task need to be done
}

can anyone please help me. Thanks in advance

question from:https://stackoverflow.com/questions/66059631/how-to-set-environment-variable-and-how-to-pass-the-environment-variable-as-a-pa

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Steps For creating runtime-environment variable In Terminal

  1. Firstly we need to install the command npm install dotenv --save-dev . This command will install dotenv module in your node modules folder.

  2. For setting up environment variable in runtime (during execution), we need to execute the command

    type=<type name> npm run test:<test name>

Eg: type=Uitest npm run test:UI

This command will create a run time variable type with value Uitest while executing the UI test

Passing this environment variable as a parameter in test.js file

async function(){
if(process.env.type=="Uitest"){
some tasks need to be done
}
else if(process.env.type!="Uitest"){
some task need to be done
}
else{
some task need to be done
}

-----While executing the code when the environment variable is with type Uitest then the set of code under if will get executed. When the environment variable is not with type Uitest then the set of code under else if will get executed.When no environment variable is set during run time

eg:npm run test:UI

then the set of code in else will get executed. Thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...