mercredi 22 avril 2015

Tests from looping through async JavaScript (Mocha)

I'm trying to test asynchronous JavaScript with Mocha, and I have some issues with looping through an asynchronously filled array.

My goal is to create N (=arr.length) tests, one for each element of the array.

Probably there's something about Mocha semantics I'm missing.

This is my (non working) simplified code so far:

var arr = []

describe("Array test", function(){

    before(function(done){
        setTimeout(function(){
            for(var i = 0; i < 5; i++){
                arr.push(Math.floor(Math.random() * 10))
            }

            done();
        }, 1000);
    });

    it('Testing elements', function(){
        async.each(arr, function(el, cb){
            it("testing" + el, function(done){
                expect(el).to.be.a('number');
                done()
            })
            cb()
        })
    })
});

The output I receive is:

  Array test
    ✓ Testing elements


  1 passing (1s)

I'd like to have an output like this one:

Array test ✓ Testing elements ✓ testing3 ✓ testing5 ✓ testing7 ✓ testing3 ✓ testing1

1 passing (1s)

Aucun commentaire:

Enregistrer un commentaire