在开始摩卡测试之前,请确保Express应用程序正在运行

问题描述

我使用express和node.js构建了一个沙发数据库的API。我的问题是,当我运行测试时,其中一些失败,因为服务器未完全运行。我在https://mrvautin.com/ensure-express-app-started-before-tests处找到了有关如何解决此问题的解决方案。文章指出,为了解决此问题,您必须像这样在服务器文件中添加事件发射器

app.listen(app_port,app_host,function () {
    console.log('App has started');
    app.emit("appStarted");
});

然后将其添加到测试文件中

before(function (done) {
    app.on("appStarted",function(){
        done();
    });
});

我已经尝试过了,这是我的实现方式

服务器文件

app.listen(config['server']['port'],function(){
    app.emit("appStarted");
    logger.info("Listening")
})

测试文件

before(function(done){
    app.on("appStarted",function(){
        done();
    })
});

我不断遇到以下错误

  1) "before all" hook in "{root}":
     Error: Timeout of 2000ms exceeded. For async tests and hooks,ensure "done()" is called; if returning a Promise,ensure it resolves.
      at listOnTimeout (internal/timers.js:549:17)
      at processTimers (internal/timers.js:492:7)

该文章来自2016年,所以我在想语法可能已被弃用。我想知道是否有人可以帮助我指出正确的方向?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)