93

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';
Ronan Quigley
  • 1,081
  • 1
  • 8
  • 13
  • 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. – Bryan Downing May 09 '18 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. – Ronan Quigley May 10 '18 at 10:23
  • docs: https://jestjs.io/docs/configuration#setupfiles-array – Louis Huang Jul 05 '22 at 02:40

10 Answers10

65

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"
  }
}
The Coder
  • 4,981
  • 2
  • 29
  • 36
  • 9
    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 '19 at 11:55
  • 16
    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 '22 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.` – MeVimalkumar Dec 12 '22 at 17:52
61

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' });
Micaël Félix
  • 2,697
  • 5
  • 34
  • 46
David Dehghan
  • 22,159
  • 10
  • 107
  • 95
24

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"
}
bobylito
  • 3,172
  • 22
  • 27
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.

li x
  • 3,953
  • 2
  • 31
  • 51
17

(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.

ZYinMD
  • 3,602
  • 1
  • 23
  • 32
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;
exaucae
  • 2,071
  • 1
  • 14
  • 24
5

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.

Ronan Quigley
  • 1,081
  • 1
  • 8
  • 13
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);

  });
});
vijayakumarpsg587
  • 1,079
  • 3
  • 22
  • 39
  • 1
    Asking OP to install additional modules or libraries when not necessary isn't a good way to approach solution. – CWSites Apr 16 '22 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

thisismydesign
  • 21,553
  • 9
  • 123
  • 126
0

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.

MeVimalkumar
  • 3,192
  • 2
  • 15
  • 26