Use .env variables in Jest & NodeJS testing

Photo by Clément H on Unsplash

If you want to test a a NodeJS application with Jest, you often need to set local environment variables. Usually these variables are set on the server when deploying your application, but they’re not available locally for testing.

A common pattern is to use a .env file with all variables for local development and testing. However, to load these variables before you run your test can be…