【问题标题】:Mocha: Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolvesMocha:错误:超过 2000 毫秒的超时。对于异步测试和钩子,确保调用“done()”;如果返回一个 Promise,确保它解析
【发布时间】:2020-05-12 05:31:22
【问题描述】:

这段代码:

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() 。

【问题讨论】:

  • 这种格式的完整凭据怎么样 - mongodb://username:password@localhost:portnum/dbName

标签: javascript mongoose mocha.js


【解决方案1】:

您可以增加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...
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-07
    • 2019-06-14
    • 1970-01-01
    • 2019-07-24
    • 2020-11-19
    • 2019-07-30
    • 2020-10-23
    • 2019-06-24
    相关资源
    最近更新 更多