From 020102cb278fc2b8b60219e3d4e8eedb0a58f61e Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Tue, 18 Feb 2020 11:05:47 +0100 Subject: [PATCH] move dotenv config from server index to the config file --- backend/src/config/index.ts | 14 ++++++++++++++ backend/src/server.ts | 4 ---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/backend/src/config/index.ts b/backend/src/config/index.ts index 2df9a83..cdb15a6 100644 --- a/backend/src/config/index.ts +++ b/backend/src/config/index.ts @@ -1,3 +1,17 @@ +import dotenv from "dotenv"; +import fs from "fs"; +dotenv.config(); +if (process.env.NODE_ENV === "test"){ + try{ + const envConfig = dotenv.parse(fs.readFileSync('.test.env')); + for (const k of Object.keys(envConfig)) { + process.env[k] = envConfig[k]; + } + }catch (e) { + //Use .env file values + } +} + const SERVER_PORT:number = parseInt(process.env.SERVER_PORT, 10) || 8080; const VALID_EXPORT_TYPES: {[key:string]: string} = { diff --git a/backend/src/server.ts b/backend/src/server.ts index 9d3dcd5..9c1c621 100644 --- a/backend/src/server.ts +++ b/backend/src/server.ts @@ -1,11 +1,7 @@ import express from "express"; -import dotenv from "dotenv"; import * as bodyParser from 'body-parser'; - import * as routes from "./routes" -dotenv.config(); - const app = express(); app.use(bodyParser.json());