3

I'm trying to use a different .env file for my Jest tests, but so far I couldn't make it work.

package.json:

{
  "name": "task-manager",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "module": "main.js",
  "scripts": {
    "start": "node -r esm src/index.js",
    "dev": "nodemon -r esm -r dotenv/config src/index.js dotenv_config_path=./config/.env",
    "test": "jest --setupFiles dotenv/config --watch"
  },
  "jest": {
    "testEnvironment": "node"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@sendgrid/mail": "^6.3.1",
    "bcryptjs": "^2.4.3",
    "dotenv": "^6.2.0",
    "esm": "^3.2.10",
    "express": "^4.16.4",
    "jest": "^24.3.1",
    "jsonwebtoken": "^8.5.0",
    "mongodb": "^3.1.13",
    "mongoose": "^5.4.17",
    "multer": "^1.4.1",
    "sharp": "^0.21.3",
    "supertest": "^4.0.0",
    "validator": "^10.11.0"
  },
  "devDependencies": {
    "@babel/core": "^7.3.4",
    "@babel/preset-env": "^7.3.4",
    "babel-jest": "^24.3.1"
  }
}

Every time I ran my npm test, the MONGODB_URL used was the stored in my .env file, instead of my test.env file

I created a config folder to store my .env dev file to avoid this, but now my app doesn't use the env vars when I run the Jest.

I setup a config path in my dev script, but I couldn't do the same with Jest.

Expected behavior: I just want to use a different MONGODB_URL for my tests with Jest.

skyboyer
  • 22,209
  • 7
  • 57
  • 64
Daniel C
  • 81
  • 1
  • 1
  • 7

3 Answers3

7

The accepted answer is quite confusing to me, I cant make it work, have to refer to this question: Using .env files for unit testing with jest

Here is how I am able to make it works:

My files structure

-src/
-tests/
------/dotenv-config.js
-jest.config.js
-.test.env
-.env

jest.config.js

module.exports = {
  setupFiles: [
    "<rootDir>/tests/dotenv-config.js"
  ],
  roots: ['<rootDir>/src'],
  testEnvironment: 'node',
  testMatch: ['**/*.test.(ts|tsx)'],
  collectCoverageFrom: ['src/**/*.{ts,tsx}'],
  preset: 'ts-jest',
};

dotenv-config.js

require('dotenv').config({
  path: '.test.env',
});

Use debug options from https://www.npmjs.com/package/dotenv to see if the .test.env loaded correctly.

vanduc1102
  • 5,769
  • 1
  • 46
  • 43
3

You have to explicitly specify which .env should be used by the dotenv package.

In your dotenv/config.js file which is used as a setup file for jest on the very first line add this:

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

nakhodkin
  • 1,327
  • 1
  • 17
  • 27
  • I create a file.js where I stored the config.path, and created a "jest": { "testEnvironment": "node", "setupFilesAfterEnv": ["/tests/dotenv-config.js"] }, to run this file, looks that it's working. Thanks and happy code! – Daniel C Mar 10 '19 at 22:20
  • 3
    Hey. I'm facing the same issue. Want to use a different env file for jest unit testing. But as you mentioned in your answer to add an absolute path in dotenv/config.js file. But I dont have any dotenv/config.js file. My .env file is in the root directory. And I called "require("dotenv").config()" in app.js file which is the express setup and I run the server by index.js file. My test.env file is in the root directory as well. Can you please help? – Zak Mar 14 '20 at 17:51
0

the easiest way to use a envFile with jest is:

on /package.json

"scripts": {
    "test": "jest"
}

on /jest.config.js

require("dotenv").config({ path: "test/.env" });

module.exports = {};

in this case, the test/.env has my environment variables for tests.

Barenko
  • 13
  • 4