How to repeat / skip mocha tests

I have been working on several mocha / chai tests, and so far I have not found a good way to run my tests in many different ways, except that I put a loop in each of the β€œit” tests and repeated it one time. The problem is that if I have dozens or hundreds of tests, I don’t want to write the same for-loop again and again.

Is there a more elegant way to do this? In particular, the one that passes 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) }); }); 
+7
tdd mocha chai
source share
1 answer

I implemented a neezer solution in Loop Mocha tests? , which includes the entire test in closing and executing it with a loop.

Remember that the loop works with the beforeEach () function inside the function, since it does this 52 times per test. Placing elements inside the beforeEach () function is not a good idea if these elements are dynamic and should not be executed more than once per cycle.

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); } 
+11
source share

All Articles