Mocha tests with extra options or parameters

I am writing test cases for my Node.js application using Mocha. The test cases need an API key as an extra input option or parameter. The API key is private, so I don't want to include it directly in the test files as everyone then can see it on GitHub. I know there are some options available for Mocha at:

http://mochajs.org/#usage

But is it possible to include some parameters to let testers specify their own API key for the test in the commandline? Such as:

./node_modules/mocha/bin/mocha test/*.js --key YOUR_KEY

Take a look at the optimist module by Substack and nconf from flatiron. A lot of my tests depend on external parameters and the optimist and nconf modules makes it easy to load configuration options from a json file

In your test command pass the path to the config.json file

test command

mocha test/api-test.js --config=/path/to/config.json --reporter spec

api-test.js

var path = require('path')
var fs = require('fs')
var assert = require('assert')
var argv = require('optimist').demand('config').argv
var configFilePath = argv.config
assert.ok(fs.existsSync(configFilePath), 'config file not found at path: ' + configFilePath)
var config = require('nconf').env().argv().file({file: configFilePath})
var apiConfig = config.get('api')
var apiKey = apiConfig.key

config.json

{
  "api": {
    "key": "fooKey",
    "host": "example.com",
    "port": 9000
  }
}

Alternative

Another pattern I have been using recently is the config module. You can specify a ./config/default.yml file for running regularly and a ./config/test.yml file for tests.

When running your test suite, export NODE_ENV=test and the config module will load test.yml

In your code it is easy to access the configuration object

var config = require('config')

// config now contains your actual configuration values as determined by the process.env.NODE_ENV
var apiKey = config.api.key

An easy way to set NODE_ENV=test is by running your tests with a makefile. Run all your tests via make test. To run a single test execute make one NAME=test/unit/sample-test.js

Sample makefile

MOCHA?=node_modules/.bin/mocha
REPORTER?=spec
GROWL?=--growl
FLAGS=$(GROWL) --reporter $(REPORTER) --colors --bail

test:
        @NODE_ENV="test" \
        $(MOCHA) $(shell find test -name "*-test.js") $(FLAGS)

one:
        @NODE_ENV="test" \
        $(MOCHA) $(NAME) $(FLAGS)

unit:
        @NODE_ENV="test" \
        $(MOCHA) $(shell find test/unit -name "*-test.js") $(FLAGS)

integration:
        @NODE_ENV="test" \
        $(MOCHA) $(shell find test/integration -name "*-test.js") $(FLAGS)

acceptance:
        @NODE_ENV="test" \
        $(MOCHA) $(shell find test/acceptance -name "*-test.js") $(FLAGS)

.PHONY: test

I don't think Mocha itself supports passing extra parameters to your tests, but you could use environment variables:

env KEY=YOUR_KEY mocha test/*.js # assumes some sort of Unix-type OS.

And read them in your test files:

var key = process.env.KEY;

There's no supported way to do this with Mocha. the suggested way is to use a file (for instance config.json), require it, and let other people change it.

That being said, if you pass your key at the end of the commandline (after the file to test) and use -- it should be available using process.argv (if you don't use -- or it's not after a regular file name, then mocha will fail).

if you run ./node_modules/mocha/bin/mocha --reporter spec test.js --apiKey=someKey , and test.js contains the code:

var assert = require("assert")
describe("testy", function () {
    it("shouldy", function (done) {
        var value;
        for (var index in process.argv) {
            var str = process.argv[index];
            if (str.indexOf("--apiKey") == 0) {
                value = str.substr(9);
            }
        }
        assert.equal(value,"someKey")
        done();
    })
})

the test should pass

I could send parameter thought mochaStream (require('spawn-mocha-parallel').mochaStream).

like:

var mochaStream = require('spawn-mocha-parallel').mochaStream;

var mocha = mochaStream({
    env: function(){
        return {yourParam: 'value'}
    }
});

return gulp.src('test/**/*-specs.js', {read: false})
    .pipe(mochaStream)
    .on('error', console.warn.bind(console));

Inside ..spec.js file

var yourParam = process.env.yourParam;