Environment variables for local server and testing (Jest)

Not super confident in JS/Node, so need some clarification regarding environment variables.

My app is running on Sashido, where I can configure Environment Variables in their dashboard. For running my server locally while developing, I’ve been using an .env file. Now, I’ve added testing with Jest and to add ENV in the tests I import a setEnvVars.js file in the jest.config.js file.

Can you recommend a way to provide the env vars for running locally and testing (JEST) in a single solution? I now rely on both the .env file and setEnvVars.js, which basically contain duplicate info.

My jest.config.js

module.exports = {
  setupFiles: ["./setEnvVars.js"]
};

Given your current setup, the easiest way is to just replace the current content of your setEnvVars.js to:

require('dotenv').config()

It will load the env vars from your existing .env file.

1 Like

That seems pretty obvious and straightforward once in front of me :slight_smile: Thanks a lot!