Reputation: 16695
This is a follow up to my How to refer to environment variables in Cypress config files? post. Steve Zodiac's answer is the correct one indeed.
So I have a config file with environment block like this
{
"env" : {
...
"db" : {
"user" : "db_user",
"password" : "pw1234",
"host" : "my_db.company.com",
"port". : 3306
}
...
}
I don't want to hard code the user/password
creds so I do this on the command line
$ npm run cy:open -- --config-file config/my_config.json --env '{"db":{"user":"db_user","password":"pw1234"}}'
and remove the user
and password
key/value pairs in my config file.
{
"env" : {
...
"db" : {
"host" : "my_db.company.com",
"port". : 3306
}
...
}
I see the correct db: user/pw
creds in the Cypress Setting->Configuration
tab of the Cypress console. However, I see this error when I run above command, that is, as if the DB creds were not correctly set.
Error: (conn=716050, no: 1045, SQLState: 28000) Access denied for user ''@'10.40.0.44' (using password: NO)
If I create a dummy variable, like this
{
"env" : {
...
"db" : {
"user" : "dummy",
"password" : "dummy",
"host" : "my_db.company.com",
"port". : 3306
}
...
}
and run my npm run cy:open...
command above, I get the following. IOW, the dummy values are not superseded by the passed-in values like I expected.
Error: (conn=782727, no: 1045, SQLState: 28000) Access denied for user 'dummy'@'10.40.0.44' (using password: YES)
What am I missing?
Upvotes: 0
Views: 2302
Reputation: 19939
--env '{"db":{"user":"db_user","password":"pw1234"}}'cyress are you sure its working ?
is not valid format are you sure its working ?
when you use -- after run , resst of the commandline options are not passed , try removing the first -- and see you will get parsing error for the
--env '{"db":{"user":"db_user","password":"pw1234"}}'
the valid format is key:value
https://docs.cypress.io/guides/guides/command-line.html#cypress-open-env-lt-env-gt
--env db={"user":"db_user","password":"pw1234"}}
Once uses JSON.parse() this will be equivalent to
"env":{
"db":{
{
"user":"db_user",
"password":"pw1234"
}
}
}
So to add the variable to existing variable only way is to update plugin file index.js
// cypress/plugins/index.js
module.exports = (on, config) => {
// modify env var value
config.env.db.password= config.env.password
// return config
return config
}
and pass password as
--env password="something"
now you can access this as
Cypress.env('db').password
Upvotes: 1