I'm developing a nodejs api in my free time, and I'm trying to implement testing now. I'm currently loading my environment variables from a .env file (loaded using dotenv
), which include the DB_URI
, DB_USER
and DB_PASSWORD
for my development mongodb database.
Now, I would like to create a separate database for testing however I don't know how would I load different variables to connect to the testing database instead of the development database. I deploy to Heroku where I have different environment variables so that's covered just fine.
I've tried to find online for some answers for best practices, but I have been unable to. I thought of creating a different .env file, however that's not recommended according to the documentation on npmjs..
Other resources recommended recommended hard coding the specific variables I needed in the package.json script. However, the script would be huge if I had to change all the variables needed to connect to a different database.
Can I get some help understanding how I should do this?
Thanks!
PS: In case it's needed, I'm using mocha
and supertest
for my tests.