I have the following setup for running the 'it' tests:
X is environment variable
if( X == "all" || X == "some value" )
read directory and run test using it() with callbacks
if( X == "all" || X == "some other value")
read directory and run some it() tests with callback
The problem I am facing is when i give "some value" or "some other value", all the it() tests run just fine. But when environment variable is "all", while running the first it() tests, the directory contents of the second if statement is appearing. I am using fs.readdirSync(dir) to read the contents and I know mochatest runs them asynchronously and hence, the content of second directory is appearing in the first tests. Is it possible to block the execution of second if statement till all the it() tests in the first if gets completed successfully? or any alternative to make it run synchronously.
In Mocha, an it() test can block until you say it is complete. Just pass a "done" callback into the test's function, like this:
it( "running test", function( done ) {
//do stuff, even async
otherMethod( function(err, result ){
//check err and result
done();
} );
} );
Mocha will run it() test serially within a describe() block as well.
The best way was to use mocha-testdata async npm module and pass it an array of files which fixes all the issues above and all the test cases run just fine without any issues : Something like
testData = require('mocha-testdata');
testData.async("array of files").test('#testing' , function(done, file) {
});
Also, use mocha suite() instead of it(),describe() calls.
suite('test:', function() {
testData.async("files array").test('#testing' , function(done, file) {
// Code that works on the list of files
});
});