Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to repeat/loop through mocha tests

Tags:

tdd

mocha.js

chai

I've been working on a few mocha/chai tests, and I still haven't found a good way of running my tests over many different possibilities aside from placing a loop within each of the 'it' tests and iterating one and a time. The problem is, if I have tens or hundreds of tests, I don't want to write the same for-loop over and over again.

Is there a more elegant way of doing this? Particularly one that loops through all the tests at once with different test parameters?

describe('As a dealer, I determine how many cards have been dealt from the deck based on', function(){

  console.log(this);

  beforeEach(function(){
    var deck = new Deck();
    var myDeck = deck.getCards();
  });


    it('the number of cards are left in the deck', function(){
      for(var i = 1; i<=52; i++){
        myDeck.dealCard();
        expect(myDeck.countDeck()).to.equal(52-i);
      }
    });

    it('the number of cards dealt from the deck', function(){
      expect(myDeck.countDealt()).to.equal(i);
    });

    it('the sum of the cards dealt and the cards left in the deck', function(){
      expect(myDeck.countDeck() + myDeck.countDealt()).to.equal(52)
    });

});
like image 300
Vincent Chan Avatar asked Feb 19 '14 19:02

Vincent Chan


People also ask

Can mocha rerun tests?

Retrying Mocha testsMocha provides a this. retries() function that allows you specify the number of times a failed test can be retried. For each retry, Mocha reruns the beforeEach() and afterEach() Hooks but not the before() and after() Hooks.

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.

How do you skip the mocha test?

You can skip tests by placing an x in front of the describe or it block, or placing a . skip after it. describe('feature 1', function() {}); describe.


Video Answer


1 Answers

I implemented neezer's solution at Loop Mocha tests?, which involves putting the entire test into a closure and executing it with a loop.

Please be aware that the loop messes with beforeEach() within the function, as it executes it 52 times per test. Placing elements within the beforeEach() function is not a good idea if those elements are dynamic, and are not to be executed more than once per loop.

The code looks like this, and it seems to work.

var myDeck = new Deck(Card);

function _Fn(val){

    describe('As a dealer, I determine how many cards have been dealt from the deck based on', function(){

      myDeck.dealCard();

      var cardCount = 0;
      var dealtCount = 0;

      cardCount = myDeck.countDeck();
      dealtCount = myDeck.countDealt();

      it('the number of cards are left in the deck', function(){
        expect(cardCount).to.equal(52-val);
      });

      it('the number of cards dealt from the deck', function(){
        expect(dealtCount).to.equal(val);
      });

      it('the sum of the cards dealt and the cards left in the deck', function(){
        expect(cardCount + dealtCount).to.equal(52);
      });

    });

}

for(var i = 1; i<=52; i++){
  _Fn(i);
}
like image 148
Vincent Chan Avatar answered Oct 20 '22 18:10

Vincent Chan