106

Is it possible to load environment variables from an env file for unit testing purposes in Jest? I'm looking to run a series of tests on it like so:

// unit tests for env file
describe('env', () => {
    it('should have a client id', () => {
        expect(process.env.CLIENT_ID).toBeDefined();
    });
    it('should have a client secret', () => {
        expect(process.env.CLIENT_SECRET).toBeDefined();
    });
    it('should have a host', () => {
        expect(process.env.HOST).toBeDefined();
    });
    it('should have a scope', () => {
        expect(process.env.SCOPE).toBeDefined();
    });
    it('should have a response type', () => {
        expect(process.env.RESPONSE_TYPE).toBeDefined();
    });
    it('should have a redirect uri', () => {
        expect(process.env.REDIRECT_URI).toBeDefined();
    });
});

Currently, all the above tests will fail, stating that the variables are undefined. Initially I was using a mocha/chai setup, which allowed me to just load all of my env variables via the use of dotenv. This involved running all unit tests through webpack and worked fine.

However, from reading the documentation Jest doesn't run tests through webpack; instead modules are mocked out via moduleNameMapper. This works fine for everything else, but I can't get the env file variables to load. So far I've tried using the setupFiles option to a js file that calls dotenv.config with the path of the env file given to it like so:

// setup file for jest
const dotenv = require('dotenv');
dotenv.config({ path: './env' });

This didn't work, so I've now resorted to using just a .env.js file for unit tests and passing this file into the setupFiles option instead. However, for maintainability, and to keep it working with webpack for production, I'd like to just keep it all in one file. Here's an extract of how the .env.js file looks for reference

// .env.js extract example
process.env.PORT = 3000;
process.env.HOST = 'localhost';
process.env.CLIENT_ID = 'your client id';
process.env.REDIRECT_URI = 'your callback endpoint';
3
  • 7
    Using Jest's setupFiles with dotenv.config(...) is the way to go for sure. What was the error you ran into with that approach? I'd double check to make sure your paths are correct. I use dotenv-safe on a project, and initialize in the jest setup file almost exactly as you have it. May 9, 2018 at 19:33
  • 2
    Yes that was pretty much it, thanks. Can't check your comment as the answer,so I've added it in just now. May 10, 2018 at 10:23

12 Answers 12

73

None of these worked for me, but I found a great article on configuring dotenv by default in Jest in the package.json:

{
  "scripts": {
    "test": "jest --setupFiles dotenv/config"
  }
}
3
  • 10
    Yep, there's no need to make a separate setup file. If you have an existing jest.config.js you can add it there as well: module.exports = { setupFiles: ['dotenv/config'] }
    – kristoffer
    Aug 21, 2019 at 11:55
  • 20
    if you are like me trying to find a way to change the dotenv config path to use .env.test instead of .env, the answer is DOTENV_CONFIG_PATH=./.env.test jest when you run jest.
    – Nikordaris
    Jan 26, 2022 at 22:35
  • @Nikordaris I tried in the same way as you mentioned in the comment. It just doesn't work for me. dotenv_config_path=.envs/local.env jest --setupFiles dotenv/config This is what I have configured for the npm test command but on the terminal, it throws the following error 'dotenv_config_path' is not recognized as an internal or external command, operable program or batch file. Dec 12, 2022 at 17:52
66

You can load the env files for all your tests like this. The setup file will be executed once per each test file that is loaded.

jest.config.js:

module.exports = {
    setupFiles: ["<rootDir>/test/setup-tests.ts"],
};

test/setup-tests.ts:

import * as dotenv from 'dotenv';

dotenv.config({ path: './config.env.test' });
0
29

I found another solution to run jest with dotenv using a custom .env file. In my case, I run jest using NPM scripts.

  1. Setup jest to use dotenv (as shown in other solutions)
jest --setupFiles=dotenv/config
  1. Add the custom .env file (here: .env.test) using a environment variable
DOTENV_CONFIG_PATH=.env.test jest --setupFiles=dotenv/config

This can be added to the script part of your package.json directly

"scripts": {
  "test": "DOTENV_CONFIG_PATH=.env.test jest --setupFiles=dotenv/config"
}
3
  • 1
    very clean method, i pick this way
    – stackdave
    Jan 20, 2022 at 9:24
  • 1
    you'll also have to configure webstorm as well under the 'debug configuration' to also add --setupFiles to the debug jest command as well Feb 22, 2022 at 19:30
  • DOTENV_CONFIG_PATH=.env.test jest --setupFiles=dotenv/config This command is either written wrong or could not be found. yesterday
22

Your top config path using ./ is relative from the point of injection, it's likely your test suite might be inside a folder named test which causes it to not be found when running your unit tests. dotenv.config(); Uses global injection strategy which is similar to absolute pathing.

1
  • Module ./__tests__/setup-test.ts in the setupFiles option was not found.
    – hellfiend
    Apr 5, 2022 at 15:26
19

(2020) According to the docs of dotenv, you shouldn't use dotenv in testing (more on that later). If you need some globally available values, there are multiple ways to set it up, for instance:

  1. setup global variables with jest:
// jest.config.json:
{
  "globals": {
    "a": 1
  }
}
// test:
test('global vars', () => {
  expect(global.a).toBe(1);
});
  1. Use a setup file:
// jest.config.json:
{
  "setupFiles": ['./jestSetup.js'],
}
// jestSetup.js:
process.env.FOO = 'FOO';
global.bar = 'bar';
// test:
test('env vars and global vars', () => {
  expect(process.env.FOO).toBe('FOO');
  expect(global.bar).toBe('bar');
});

  1. use globalSetup and globalTeardown: Very similar to 2.

The reason against using dotenv is that values in process.env are meant to be different in different deployments, which are set by the environment, not by you. If a variable doesn't change in different environments, then it's not an environmental variable, it's just a global variable that you set manually. The dotenv doc further points to the 12 factor app which is a good read.

6
  • Great answer. I would add also that having tests for environment variables passed might be useless
    – uladzimir
    Feb 18, 2021 at 19:14
  • 2
    I don't see anything stating to not use during testing. Mar 22, 2021 at 22:31
  • @AlfredoR It says to not have more than one, and gives .env.test as an explicit example of what not to do.
    – MEMark
    May 16, 2021 at 18:08
  • @MEMark if you stick to something not being a standard you end up limiting your options. the rule is good for deployment envs but I don't see harm using diff .env files for diff local development purposes such as unit or integration testing. May 16, 2021 at 23:22
  • @AlfredoR personally I tend to agree with you. But the article linked to does not share this view.
    – MEMark
    May 17, 2021 at 4:40
7

Just adding setupFiles: ['dotenv/config'] in jest.config.ts worked for me.

Useful article found here.

Full setup:


const jestConfig = {
    preset: 'ts-jest',
    globals: {
        'ts-jest': {
            tsconfig: '<rootDir>/tsconfig.spec.json',
        },
    },
    verbose: true,
    testMatch: ['<rootDir>/test/**/*(*.)+(test).+(tsx)'],
    setupFiles: [
        'dotenv/config'
    ],
    setupFilesAfterEnv: ['<rootDir>/test/setupTests.ts'],
    moduleFileExtensions: ['js', 'ts', 'tsx'],
    collectCoverage: true,
    coverageDirectory: 'target/coverage',
    collectCoverageFrom: [
        'src/**/*.tsx',
    ],
    moduleNameMapper: {
        '^.+\\.(css)$': 'identity-obj-proxy',
        '^.+\\.(png|svg|pdf|jpg|jpeg)$': 'jest-transform-stub'
    },
    transform: {
        '^.+\\.(js|jsx)$': 'babel-jest',
    },
    transformIgnorePatterns: ['[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs)$'],
};

export default jestConfig;
6

So the issue require changing this:

dotenv.config({ path: './env' });

to :

dotenv.config();

Why it didn't pick it up I have no idea, but just leaving the defaults worked.

3
  • 12
    aren't you missing a point? dotenv.config({ path: './.env' }); Oct 27, 2020 at 14:07
  • 1
    oh you, such pun 🤭
    – Qwerty
    Mar 11, 2021 at 20:07
  • Thanks for giving me the hint on path option, but the file is .env. By default it's { path: '.env' }. I have used { path: '.env.test' } to load custom testing env.
    – KeitelDOG
    Apr 5, 2022 at 15:25
3

I am little late, but hope this helps for someone who are looking for making dotenv work. This is just an extended flavor of what @DavidDehghan said. So add a setup file to be run before any jest configuration like as David said

Now making sure dotenv load for the platform to resolve the relative path, please make use of path module to resolve the folder

import * as  dotenv from 'dotenv';
import * as path from 'path';

dotenv.config({ path: path.resolve(__dirname + './../config/testFolder/.env') });

Now in your spec file you can test if the process.env variable contains the values loaded from .env file in testFolder

describe('Test suite - testing app service', () => {
  beforeAll(() => {
    console.log('coming inside beforeAll', process.env.LCp);

  });
});
1
  • 1
    Asking OP to install additional modules or libraries when not necessary isn't a good way to approach solution.
    – CWSites
    Apr 16, 2022 at 15:15
3

Combining a few answers here into a simple solution: add dotenv-flow and use it in your test script:

jest --setupFiles dotenv-flow/config

2

I'am using dotenv and I have the below structure in my package.json file.

{
  "name": "project-name",
  "scripts": {
    "test": "DOTENV_CONFIG_PATH=test.env jest --setupFiles dotenv/config "
  },
  "jest": {
    "verbose": true,
    "bail": true
  }
}

This basically allows me to use dotenv with jest, and where I can mention the exact .env file name or path.

0

I keep seeing a dependency on dotenv...

If you're trying to do this without a installing a package, simply read the text file and load it into process.env:

  1. Create a loadEnvironment.js:
let fs = require('fs')

let envFile = fs.readFileSync("path/to/env/file").toString()

let envFileLines = envFile.split("\n")

for (let line of envFileLines) {
    let [key, value] = line.split("=");
    process.env[key] = value
}
  1. Add your file to the setupFile option, for example, in jest.config.js, add: setupFiles: ["./loadEnvironment.js"],
0

If you are using nx to run your tests with a command like:

nx run-many --target=test

Then nx completely overrides dotenv with its own set of environment variables.

If you have these files in your project root:

.env
.env.test

In this case .env.test is accepted by nx and will override .env, but if you rename .env.test to anything else and set that in the dotenv path config, that will be overridden by an .env file.

I think if you have no .env (or any others file recognised by nx) then potentially your dotenv path will work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.