1
votes

I am running tests with mocha. I have the following config file:

{
  "diff": true,
  "extension": [
    "js"
  ],
  "package": "./package.json",
  "reporter": "spec",
  "slow": 75,
  "timeout": 2000,
  "ui": "bdd",
  "recursive": true,
  "exit": true,
  "require": "esm, dotenv/config"
}

As all of my code is using import statements, I am using the 'esm' package to get this to work. I am also using the 'dotenv' package to load my .env files.

I just split my .env file for testing, development, and production. However, I cannot get mocha to load the correct .env file. From dotenv documentation the is a command line argument (dotenv_config_path) that your can use to specify the .env file:

$ node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/your/env/vars

I have tried to add this into the config as well as the command line, without success.

Command line:

mocha -r dotenv/config -r esm --recursive --exit dotenv_config_path=./env/dev-config.env

but get the following error:

Error: No test files found: "dotenv_config_path=./env/dev-config.env"

However, if there is a .env file in the root directory this command works:

mocha -r dotenv/config -r esm --recursive --exit 

So how can I get a custom .env file to load?

1

1 Answers

0
votes

Figured it out. Add this to the mocha options file:

"env" : "dotenv_config_path=path/to/env/file"