Running mocha tests synchronously
Solution 1
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
});
});
Solution 2
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.
learn_develop
C/C++ Developer, bit of Android development and aiming towards becoming a full stack developer.
Updated on June 29, 2022Comments
-
learn_develop almost 2 years
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.
-
learn_develop over 9 yearsI do have a done callback but the problem is there are multiple if statements and there are several it() block with done within it. Each if reads a directory based on some env variable, so since mocha runs asynchronously and when env variable is "all", it keeps reading the directories in each if, overwriting the contents of the first if() statement, and hence, the test fails...
-
clay over 9 yearsUse different variables outside the
if
blocks so they do not get overwritten. (Hard to see without the code, though). Also, when running Mocha, there is the--grep
option, and you could label yourdescribe
blocks to match a pattern. -
learn_develop over 9 yearsusing different variable names for every if block does solve the issue @clay . However, not sure if there is a better alternative to it.
-
clay over 9 yearsWell, this is just a test environment. No need to get complicated : ) You could empty the variable at the end of an if-block as well, if you wanted to, to reuse the same variable. If a test fills a variable, and the next test uses/changes that variable and then fails it's test condition . . . that's just a matter of test setup and you've gotten around that already. Happy testing!