![](/img/trans.png)
[英]Error: Timeout of 30000ms exceeded. For async tests and hooks, ensure “done()” is called; if returning a Promise, ensure it resolves
[英]Mocha: Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves
這段代碼:
const mongoose=require('mongoose');
mongoose.connect('mongodb://localhost/users_test',{useNewUrlParser:true});
mongoose.connection
.once('open',()=>console.log('Good to go!'))
.on('error',(error)=>{
console.log('Warning',error);
})
beforeEach((done)=>{
mongoose.connection.collections.users.drop(()=>{
done();
})
})
產生錯誤:超過 2000 毫秒超時。 對於異步測試和鈎子,確保調用“done()”; 如果返回 Promise,請確保它已解決。
有誰知道為什么? 據我所知, mongoose.connection.collections.users.drop(()=>{ done(); }) 應該幾乎立即調用 done() 。
您可以增加Mocha
測試的超時時間,因為mongoose.connection.collections
是一個異步函數, network call
可能需要更長的時間才能完成。
例子。
const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/users_test", { useNewUrlParser: true });
mongoose.connection
.once("open", () => console.log("Good to go!"))
.on("error", error => {
console.log("Warning", error);
});
describe("something", function() {
this.timeout(5000);
beforeEach(done => {
mongoose.connection.collections.users.drop(() => {
done();
});
});
// tests...
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.