Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access mocha options from within a test file?

I am running mocha tests using gruntjs and grunt-simple-mocha: https://github.com/yaymukund/grunt-simple-mocha

How can I access the options defined in my grunt.js file within each mocha test?

What I would like to accomplish, is to have some common configuration in my gruntfile, and use that in my tests.

like image 294
Marek Miettinen Avatar asked Sep 19 '12 11:09

Marek Miettinen


People also ask

How do I specify a test file in Mocha?

The nice way to do this is to add a "test" npm script in package. json that calls mocha with the right arguments. This way your package. json also describes your test structure.

How do I run multiple test files in Mocha?

function importTest(name, path) { describe(name, function () { require(path); }); } var common = require("./common"); describe("top", function () { beforeEach(function () { console. log("running something before each test"); }); importTest("a", './a/a'); importTest("b", './b/b'); after(function () { console.

Does Mocha run tests 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.


2 Answers

The one way I found already is using global values, which is not very good, but works

inside grunt.js config

global.hell = 'hey you';

inside test

console.log(global.hell);

inspecting one more way now, maybe it will be better

--EDIT

No, seems it's the one I will stop at, if I don't want to end up with some black magic like in mocha-as-promised, because i don't have skills to write that.

--EDIT

Also you can take a look at this - https://github.com/visionmedia/mocha/wiki/Shared-Behaviours you can share some object between tests, but not sure if it will help with grunt

like image 60
llamerr Avatar answered Oct 18 '22 11:10

llamerr


As far as I'm aware there is no way to push any objects into your mocha suit. The only other interpretation I can think of for your question, you would like to load a common set of configs among your test files. I dont belive you can, other than at the very top of your test files loading a common config file to be availble to your test methods.

like image 43
Miguel Coquet Avatar answered Oct 18 '22 11:10

Miguel Coquet