This is a function to create an asynchronous helper. This helps for helpers that perform mocha, db preparers, among others.
How does it work
It stops Mocha#rut()
preventing the upload of the files. it execute an asynchronous function and the continues with the mocha loader.
Copy this chunk in your helper
Prefer in the end of the file.
function preloadMocha(asyncFn) {
const Mocha = require('mocha/lib/mocha');
const run = Mocha.prototype.run;
Mocha.prototype.run = function preRun(...args) {
Promise.resolve(asyncFn()).then(() => {
return run.apply(this, args);
});
};
}
Sample
// helper.js
const sanbox = sinon.createSandbox();
const mongod = new MongoMemoryServer();
preloadMocha(async () => {
const mongouri = await mongod.getConnectionString();
sanbox.replace(process.env, 'MONGO_URI', mongouri);
});
Load with mocha
$ mocha -r helper.js