Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using .env files for unit testing with jest

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'; 
like image 320
Ronan Quigley Avatar asked May 09 '18 17:05

Ronan Quigley


People also ask

Should you commit .env files?

env file should be committed to version control. The trouble is that blindly committing it creates a huge security risk. Here I discuss a few pointers on how to do so in a safe manner.

When should I use an .env file?

env file should be particular to the environment and not checked into version control. This. env. example file documents the application's necessary variables and can be committed to version control.

Where does .env file react to?

The most common approach to define EVs in a React application is to store them in a plain text file with a . env extension located at the root of your project — that is, within the same directory as the package. json file.


2 Answers

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"   } } 
like image 167
The Coder Avatar answered Oct 14 '22 01:10

The Coder


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' }); 
like image 24
David Dehghan Avatar answered Oct 13 '22 23:10

David Dehghan