我正在尝试测试Express应用程序,并且需要先确保该应用程序已启动,然后再运行任何测试。因此,我试图使应用在就绪时发出事件,并在我的before()
中等待该事件,但是由于某种原因,我从未收到该事件并得到Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
。我在做什么错了?
Index.js
const express = require('express')
const app = express()
const port = 3000
app.listen(port, function() {
console.log(`app listening on port ${port}!` )
app.emit('appStarted')
})
module.exports = app
test.js
const app = require('../index.js');
before(done => {
console.log('BEFORE')
app.on('appStarted', () => {
done()
})
});