Using .env files for unit testing with jest
JavascriptUnit TestingEnvironment VariablesJestjsJavascript Problem Overview
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';
Javascript Solutions
Solution 1 - Javascript
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"
}
}
Solution 2 - Javascript
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 dotenv from 'dotenv';
dotenv.config({ path: './config.env.test' });
Solution 3 - Javascript
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.
Solution 4 - Javascript
(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:
- setup global variables with jest:
// jest.config.json:
{
"globals": {
"a": 1
}
}
// test:
test('global vars', () => {
expect(global.a).toBe(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');
});
- 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.
Solution 5 - Javascript
I found another solution to run jest with dotenv using a custom .env
file. In my case, I run jest using NPM scripts.
- Setup jest to use dotenv (as shown in other solutions)
jest --setupFiles=dotenv/config
- 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"
}
Solution 6 - Javascript
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;
Solution 7 - Javascript
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.
Solution 8 - Javascript
Combining a few answers here into a simple solution: add dotenv-flow
and use it in your test script:
jest --setupFiles dotenv-flow/config
Solution 9 - Javascript
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);
});
});