Elegant ways to pass env variables to Mocha test cases.

Sandeep
NodejsMadeEasy
Published in
3 min readJan 31, 2021

--

Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun.

We often use environment variables in our code(modules) and when testing code it should be available at run time. In this post, I will show you a few nice ways to pass environment variables to Mocha test cases. You can choose one suits your need best.

Using `- -require (-r)` option

Mocha provides you rich set of CLI options. You can use the --require (-r) command-line option to preload dotenv . In your package.json file use this test command:

"scripts": {
"test": "mocha tests/**/*.js -r dotenv/config"
},

Note: .env file should be in the current working directory(Project root).

Using env-cmd

env-cmd is a simple node program for executing commands using an environment from an env file. It provides more controls on how you pass and use env variables.

Basic Uses:

It uses an environment file ./.env

# This is a commentSECRET=1334
API_KEY=apikey123

and Package.json

{
"scripts": {
"test": "env-cmd mocha tests/**/*.js"
}
}

Advanced Usage:

For more complex projects, a .env-cmdrc file can be defined in the root directory and supports as many environments as you want. Simply use the -e flag and provide which environments you wish to use from the file.

./.env-cmdrc

{
"development": {
"ENV1": "env1",
"ENV2": "env2"
},
"test": {
"ENV1": "env1test1",
"ENV3": "envtest3"
},
"production": {
"ENV1": "production"
}
}

You can use test command like this:

"scripts": {
"test": "env-cmd -e test mocha tests/**/*.js"
},

here option `e` specifies an environment to be used. You can also use multiple environments

"test": "env-cmd -e test,development mocha tests/**/*.js"

Using multiple environment names will merge the environment variables together. Later environments overwrite earlier ones in the list if conflicting environment variables are found.

Using Mocha Global Hook

Creating a solid test suite for your application may require one or more setup steps before running the tests.

Mocha offers two ways for this:

  1. Global hooks
  2. root-level hooks in single test files that run before tests or beforeEach individual test

Global Setup

A global setup hook may run before any of your tests. The global hook requires one or more setup files.

For example, suppose you want to setup env programmatically use can create setup-env-hook.js a file and place it in the test folder

//setup-env-hook.jsbefore(()=>{
require('dotenv').config()
})

When you run test command, setup-env-hook.js will be executed before any test, provide you access to the environment variable required for the test cases.

Now suppose you want to run a unit test case also for mongoose models. In order to do so, you will connect to the database first but also need to load the env first to get database details. If you need setup steps that rely on each other, you can tell Mocha to import them sequentially and process them in the expected order.

You can use --file option via npm script or create a file called mocha.opts with this configuration

--file ./test/setup-env-hook.js
--file ./test/connect-to-db-hook.js

Above global hooks will be executed sequentially before any other test suites.

I hope you liked reading it. Any constructive feedback is appreciated.

.……………………………………🙏………………………………………….

--

--