Somebody knows how to fix this: http://stackoverflow.com/questions/37389478/mocha-use-data-from-a-previous-test ?
Maybe I can do all the job inside one unique it-case (so use a loop inside the it-case). But in that way I'll have to deal with timeout issues probably. Perhaps better have a this.timeout() increased linearly?
You probably need something like this, run with the delay option (mocha.delay() in the browser, --delay on the commandline), which allows mocha to wait to run till you call run() to tell it you're done adding tests:
getDataFromSomewhereAsynchronously(function(data){
describe('using asynchronously acquired data', function(){
// This is equivalent to the test where you were setting x in your question.
it('always-defined test using asynchronously acquired data', function() {
assert.whateverYouNeedToCheck(data);
});
data.forEach(function(dataItem, index){
it('dynamically generated test number ' + (index + 1), function(){
assert.whateverYouDoWithThisData(dataItem);
});
});
run();
});
});
@ScottFreeCode you're right. That seems the best way to do it. Thanks.
Looks like Louis on Stack Overflow is right about the its and the describe block execution; you'd need to put the whole describe inside the callback. I've editted my example to reflect this.
Tangentially, if you could link to this from your Stack Overflow answer just to be sure it's clear which parts are from my suggestion, I'd appreciate it; thanks!
Edit queue for the SO is full, added link to comment, question has been answered, closing.