DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,274 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for How to use environment variables when testing with Jest inΒ NodeJs
Tosin Moronfolu
Tosin Moronfolu

Posted on

How to use environment variables when testing with Jest inΒ NodeJs

I was trying to use my environment variables in my test suites and it was a hustle. After a long time of trying and researching, I finally found a way to do it and I decided to write this article to help save you time, lol.

Funny how it's quite easy and I wonder why it took me so long to get it done. Well, we learn every day.

I'm using dotenv for the environment variables and jest for testing and all you need to do is add the dotenv config to the jest setup files in your package.json file. Yes, it's that easy

{
  "name": "your-application-name",
  "jest": {
    "setupFiles": ["dotenv/config"]
  }
}
Enter fullscreen mode Exit fullscreen mode

I hope with this short article I've been able to help you save time in configuring dotenv with jest, lol.

Cheers πŸŽ‰

links:
https://jestjs.io/
https://www.npmjs.com/package/dotenv

Top comments (1)

Collapse
 
beadle85 profile image
Beadle

Thank you so much for taking the time to share this!

Classic DEV Post from 2020:

js visualized

πŸš€βš™οΈ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! πŸ₯³

Happy coding!