lundi 22 mai 2017

MochaJS is not waiting for async function

I want to write tests of an async function.

// Function declaration
someLongAsyncTasks(cb) {
    // This function doing some long tasks like fs.copy(...)
    // I will use setTimeout to simulate these jobs
    setTimeout(function() {
        cb();
    }, 3000);
}

// in tests.js file
it('...', function(done) {
    someLongAsyncTasks(function(err) {
        if (err) done(err);
        else done();
    });
});

As you see I'm passing done to it()'s callback function so it should wait until someLongAsyncTasks() finish its job. But I'm still getting this error:

Error: Timeout of 30000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

What I'm missing? Why it() is not waiting for my async function and firing timeout error?

Aucun commentaire:

Enregistrer un commentaire