【发布时间】:2021-08-27 16:23:48
【问题描述】:
在测试快速应用程序的默认错误处理程序时,会导致超时。函数如下:
const createApp = (underlyingFunction) => {
const app = express()
app.get('/my-endpoint', async (req, res) => {
await underlyingFunction()
res.send({ success: true })
})
const errorHandler: ErrorRequestHandler = (error, req, res, next) => {
console.error('Unhandled exception');
console.error(error);
console.error(error.stack);
res.status(500).send({
message: 'Oh dear',
});
// next()
}
app.use(errorHandler)
return app;
}
测试如下:
test('error should be handled and return 500', async () => {
underlyingFunction.mockImplementation(() => {
throw new Error('Something went wrong')
})
const app = createApp(underlyingFunction)
const response = await request(app).get('/my-endpoint')
expect(response.status).toBe(500)
})
运行测试时,出现以下错误:
thrown: "Exceeded timeout of 5000 ms for a test.
Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."
这可能是什么原因造成的?
【问题讨论】:
-
underlyingFunction和createApp看起来像什么?创建一个最小的、可重现的示例 -
@slideshowp2 感谢您的反馈。我已经更新了问题。
标签: node.js express jestjs supertest ts-jest