Answer the question
In order to leave comments, you need to log in
How can you return a value from an asynchronous function in the before block in mocha so that you can use this value in the same describe in the function?
Hello!
Faced a problem when writing tests. I would like to cut down on the amount of repetitive code by putting it in a function that accepts values.
Test pseudocode:
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(done){
asyncFunction(function(err, value){
...
_value = value
...
done();
}
})
repeatTests(_value) // value is undefined
})
describe ('test', function(){
var _value
before(function(done){
asyncFunction(function(err, value){
...
_value = value
...
done();
}
})
it ('test1 use value', function(done){
console.log(_value); // return _value
...
}
it ('test2 use value', function(done){
console.log(_value); // return _value
...
}
...
})
array.forEach
it, it’s also not an option, since there will be others repeatTests
in the same.
It seems like a banal task, but somehow you can’t guess how to do it) describe
it
Answer the question
In order to leave comments, you need to log in
Alternatively, use async.waterfall()
https://www.npmjs.com/package/async#waterfall
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question