Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 
like image 255
afterglowlee Avatar asked Apr 22 '13 10:04

afterglowlee


People also ask

Do mocha tests run sequentially?

According to it, tests are run synchronously. This only shows that ordered code is run in order. That doesn't happen by accident.

How do you run a mocha test in parallel?

Mocha does not run individual tests in parallel. That means if you hand Mocha a single, lonely test file, it will spawn a single worker process, and that worker process will run the file. If you only have one test file, you'll be penalized for using parallel mode. Don't do that.

Is mocha a runner test?

Mocha is one of the most popular testing frameworks for JavaScript. In particular, Mocha has been the test runner of choice in the Node.

How do I run a specific test file in mocha?

Run a Single Test File Using the mocha cli, you can easily specify an exact or wildcarded pattern that you want to run. This is accomplished with the grep option when running the mocha command. The spec must have some describe or it that matches the grep pattern, as in: describe('api', _ => { // ... })


1 Answers

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; 
like image 162
robertklep Avatar answered Sep 28 '22 03:09

robertklep