Using .env files for unit testing with jest
Asked Answered
P

12

107

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';
Protuberate answered 9/5, 2018 at 17:31 Comment(3)
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.Stillage
Yes that was pretty much it, thanks. Can't check your comment as the answer,so I've added it in just now.Protuberate
docs: jestjs.io/docs/configuration#setupfiles-arrayCannikin
E
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.

Elastance answered 10/5, 2018 at 10:30 Comment(1)
Module ./__tests__/setup-test.ts in the setupFiles option was not found.Lajuanalake
A
75

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"
  }
}
Albanese answered 21/1, 2019 at 1:45 Comment(3)
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'] }Noctiluca
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.Tightlipped
@Tightlipped 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.Diadelphous
D
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' });
Disallow answered 8/3, 2019 at 6:0 Comment(0)
L
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"
}
Luttrell answered 11/1, 2022 at 11:14 Comment(5)
very clean method, i pick this wayTammy
you'll also have to configure webstorm as well under the 'debug configuration' to also add --setupFiles to the debug jest command as wellDerek
DOTENV_CONFIG_PATH=.env.test jest --setupFiles=dotenv/config This command is either written wrong or could not be found.Appendicular
@Appendicular how did you run the command? This is intended to be run within a NPM script or in the command line with jest installed globally.Luttrell
I ran this via 'npm run scripts' just to see if it works (with the .env.test file in the same directory like package.json). But the actual fix for me was changing setupFiles to setupFilesAfterEnvAppendicular
E
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.

Elastance answered 10/5, 2018 at 10:30 Comment(1)
Module ./__tests__/setup-test.ts in the setupFiles option was not found.Lajuanalake
E
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.

Endemic answered 3/9, 2020 at 19:48 Comment(6)
Great answer. I would add also that having tests for environment variables passed might be uselessGrimm
I don't see anything stating to not use during testing.Sourdine
@AlfredoR It says to not have more than one, and gives .env.test as an explicit example of what not to do.Anagnorisis
@Anagnorisis 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.Sourdine
@AlfredoR personally I tend to agree with you. But the article linked to does not share this view.Anagnorisis
Downvoted due to the opening statement of this answerBuccinator
W
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;
Wick answered 23/3, 2021 at 14:59 Comment(0)
P
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.

Protuberate answered 10/5, 2018 at 10:22 Comment(3)
aren't you missing a point? dotenv.config({ path: './.env' });Adamek
oh you, such pun 🤭Iseabal
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.Levania
U
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);

  });
});
Upheave answered 6/7, 2019 at 9:10 Comment(1)
Asking OP to install additional modules or libraries when not necessary isn't a good way to approach solution.Addie
V
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

Vestment answered 29/4, 2021 at 11:7 Comment(0)
D
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.

Diadelphous answered 10/10, 2022 at 13:57 Comment(0)
A
2

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.

Antihistamine answered 21/3 at 13:46 Comment(0)
B
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"],
Ballance answered 16/9, 2023 at 18:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.