jeudi 9 juillet 2015

How return value from async function in before in mocha and use same value in same describe in function?

Faced with the problem when writing code. I want to reduce the amount of code used in the tests, but there are problems.

I have test, but he don't worked:

var repeatTests = function(value){

    it ('test1 use value', function(done){
        console.log (value) // return undefined
        ...
    }

    it ('test2 use value', function(done){
       console.log (value) // return undefined
        ...
}

    ...

}

describe ('test', function(){

    var _value
    before(function(){

        asyncFunction(function(err, value){
            ...    
            _value = value
            ...

        }

    })

    repeatTests(_value) // value is undefined


})

But this worked:

describe ('test', function(){

    var _value
    before(function(){

        asyncFunction(function(err, value){
            ...
            _value = value
            ...
        }

    })

    it ('test1 use value', function(done){
        console.log(_value); // return _value
        ...
    }

    it ('test2 use value', function(done){
        console.log(_value); // return _value
        ...
    }

        ...


})

I know why this is happening. Please tell me, how do I implement the first version of the test.

Aucun commentaire:

Enregistrer un commentaire