Skip to content
Chandan Kumar

Random tech notes

tech notes1 min read

2024-07-03

While writing test case, I needed to set environment variables for Jest. I don't want to setup dotenv in the spec.

Setting Environment Variables for Jest with dotenv

This setup ensures that environment variables from a .env file are loaded before running Jest tests:

  1. Update package.json: Modify the test script to run jest.
  2. Install dotenv: Add the dotenv package to manage environment variables.
  3. Create jest.config.js: Load environment variables by requiring dotenv in the Jest configuration file.
  4. Ensure .env file: Place your environment variables in a .env file in the project root.
  • Update package.json:

    1"scripts": {
    2 "test": "jest"
    3}
  • Install dotenv:

    1npm install dotenv
  1. Create jest.config.js:

    1require('dotenv').config();
    2
    3module.exports = {
    4 // Jest config options
    5};
  2. Ensure .env file is in the project root.

Comments

Copyleft. WTH
Theme by LekoArts