2017-05-22 102 views
0

我想編寫異步函數的測試。MochaJS不等待異步函數

// 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(); 
    }); 
}); 

正如你看到的,我路過doneit()的回調函數,所以應該等到someLongAsyncTasks()完成其工作。但我仍然得到這個錯誤:

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

我失蹤了什麼?爲什麼it()未在等待我的異步功能並觸發超時錯誤?

回答

0

好的,我會回答我自己的問題。我解決了這個問題。在實際的someLongAsyncTasks()函數中有很多不同的情況,其中一個函數不調用回調函數。

在短期內,

檢查someLongAsyncTasks()函數是調用回調函數在每一種情況。

+0

另請確保您的問題中的代碼確實存在您試圖解決的問題。 – robertklep