Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Mocha setup before each suite rather than before each test

Using NodeJS and Mocha for testing. I think I understand how before() and beforeEach() work. Problem is, I'd like to add a setup script that runs before each "describe" rather than before each "it".

If I use before() it will run only once for the entire suite, and if I use beforeEach() it will execute before every single test, so I'm trying to find a middle ground.

So, if this is my test file:

require('./setupStuff');  describe('Suite one', function(){   it('S1 Test one', function(done){     ...   });   it('S1 Test two', function(done){     ...   }); }); describe('Suite two', function(){   it('S2 Test one', function(done){     ...   }); }); 

I'd like to have "setupStuff" contain a function that runs before 'Suite one' and 'Suite two'

Or, in other words, before 'S1 Test one' and 'S2 Test one' but NOT before 'S1 Test two'.

Can it be done?

like image 972
FuzzyYellowBall Avatar asked Sep 29 '14 19:09

FuzzyYellowBall


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 skip the Mocha test?

This inclusive ability is available in Mocha by appending . skip() to the suite or to specific test cases. The skipped tests will be marked as "pending" in the test results.

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

There's no call similar to beforeEach or before that does what you want. But it is not needed because you can do it this way:

function makeSuite(name, tests) {     describe(name, function () {         before(function () {             console.log("shared before");         });         tests();         after(function () {             console.log("shared after");         });     }); }  makeSuite('Suite one', function(){   it('S1 Test one', function(done){       done();   });   it('S1 Test two', function(done){       done();   }); });  makeSuite('Suite two', function(){   it('S2 Test one', function(done){     done();   }); }); 
like image 68
Louis Avatar answered Sep 30 '22 10:09

Louis


you can also do it in this more flexible way:

require('./setupStuff');  describe('Suite one', function(){   loadBeforeAndAfter(); //<-- added   it('S1 Test one', function(done){     ...   });   it('S1 Test two', function(done){     ...   }); }); describe('Suite two', function(){   loadBeforeAndAfter();//<-- added   it('S2 Test one', function(done){     ...   }); }); describe('Suite three', function(){   //use some other loader here, before/after, or nothing   it('S3 Test one', function(done){     ...   }); });  function loadBeforeAndAfter() {   before(function () {     console.log("shared before");   });   after(function () {     console.log("shared after");   }); } 
like image 25
ya_dimon Avatar answered Sep 30 '22 08:09

ya_dimon