Mocha tests with extra options or parameters

2019-01-13 14:13发布

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

标签: node.js mocha
9条回答
你好瞎i
2楼-- · 2019-01-13 14:55

A simple way, using process.argv that contain the command line args

$ mocha  -w test/*.js --KEY=YOUR_KEY

Later, you can get YOUR_KEY in your code:

let LAST_PARAM = process.argv[process.argv.length-1]

let PARAM_NAME  = LAST_PARAM.split("=")[0].replace("--","")
let PARAM_VALUE = LAST_PARAM.split("=")[1]

console.log("KEY: ", PARAM_VALUE)

To see all process.argv

process.argv.forEach((value, index) => {
        console.log(`process.argv[${index}]: ${value}`);
})

Output

$ mocha  -w test/*.js --KEY=YOUR_KEY

KEY:  YOUR_KEY
process.argv[0]: /usr/local/bin/node
process.argv[1]: /Users/pabloin/.npm-packages/lib/node_modules/mocha/bin/_mocha
process.argv[2]: -w
process.argv[3]: test/tt.js
process.argv[4]: test/tt2.js
process.argv[5]: --KEY=YOUR_KEY

KEY:  YOUR_KEY
process.argv[0]: /usr/local/bin/node
process.argv[1]: /Users/pabloin/.npm-packages/lib/node_modules/mocha/bin/_mocha
process.argv[2]: -w
process.argv[3]: test/tt.js
process.argv[4]: test/tt2.js
process.argv[5]: --KEY=YOUR_KEY
查看更多
Emotional °昔
3楼-- · 2019-01-13 14:58

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;
查看更多
▲ chillily
4楼-- · 2019-01-13 15:04

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;
查看更多
Bombasti
5楼-- · 2019-01-13 15:04

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

查看更多
贪生不怕死
6楼-- · 2019-01-13 15:10

I have been reading quite some answers, most of them more complex than the actual solution has to be.

Let's say I have config.yml or config.json. In my case it's a YAML file.

First of all I install the yamljs dependency. It has a function called load.

Basically what I do:

const YAML = require('yamljs'); const ymlConfig = YAML.load('./config.yml');

Then I go for:

process.env.setting1 = ymlConfig.setting1; process.env.setting2 = ymlConfig.setting2;

And of course - this is all done in your test file.

查看更多
疯言疯语
7楼-- · 2019-01-13 15:13

You can pass an argument to mocha test script using 'minimist' module. Install with npm install minimist

Terminal:

mocha test.js --config=VALUE

Mocha node script:

var argv = require('minimist')(process.argv.slice(2));
console.log('config', argv.config);
查看更多
登录 后发表回答