【发布时间】:2020-09-10 04:04:28
【问题描述】:
我使用 express 和 node.js 为 couchbase 数据库构建了一个 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 年的,所以我想也许语法已被弃用。我想知道是否有人可以帮我指出正确的方向?
【问题讨论】:
标签: node.js express mocha.js couchbase supertest