Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run some initialization code with mocha

I know about before, beforeEach, after and afterEach but how do I run some code before ALL tests.

In other words I files like this

test
  test1.js
  test2.js
  test3.js

I run the tests with

mocha --recursive

I don't want to have to put a before in every test file. I need a beforeAllTests or a --init=setup.js or something that I can execute some JavaScript before any tests have been executed. In this particular case I have to configure my system's logging module before the tests run

Is there a way to call some init function that get executed before all tests?

like image 654
gman Avatar asked Sep 07 '14 19:09

gman


1 Answers

If only initialization code is required, then mocha -r ./init might be enough for you. even put it into test/mocha.opts

--require ./test/init
--ui tdd

But if you need teardown actions, there is a dilemma, for example:

var app = require('../app.js');
app.listen(process.env.WEB_PORT);

after(function(done) {
    var db = mongoskin.db(process.env.DB_URL, {safe:true});
    db.dropDatabase(function(err) {
        expect(err).to.not.be.ok();
        db.close(done);
    });
});

you'll get the error:

ReferenceError: after is not defined

I guess mocha hasn't been initialized itself while handling '--require'.

like image 74
Simon Shi Avatar answered Oct 02 '22 04:10

Simon Shi