5
 describe('some test', function() {
  for(i = 0; i < someData.length; i++) {
   it("test scenario "+i, function() {
   assert.deepEqual(someValue, someData[i]);
   });
  }
 });

Having the above code is not printing mutiple pass results. It is printing the below (in green color) in the console.

0 passing (42ms)
krish
  • 186
  • 1
  • 5
  • Possibly related: https://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example – Taplar Oct 14 '19 at 19:55

1 Answers1

5

All the details are here: https://github.com/mochajs/mocha/issues/3074

Mocha doesn't support such behavior. The two most famous workarounds are:

  • IIFE
  • forEach

I would the forEach to be slightly more elegant, here is the possible solution by Scott Santucci (github), and modified by me for your case:

someData.forEach(function(value, i) {
  it(`test scenario ${i}`, function() {
    assert.deepEqual(testValue, value);
  })
})
r.delic
  • 795
  • 7
  • 18
Dragos Strugar
  • 1,314
  • 3
  • 12
  • 30
  • 1
    Sorry, but I didn't understand "array.push(0)" line. What's the point of pushing 0? Did you mean i? Can you please explain a bit more? – krish Oct 14 '19 at 20:06
  • It's just an arbitrary value. It serves the purpose of just having `someData.length` elements in the `array` array. Then you can do your looping thing. – Dragos Strugar Oct 14 '19 at 20:08
  • @DragosStrugar Why does he have to create another array, can't he just loop over someData directly? – r.delic Oct 14 '19 at 20:40