【发布时间】:2019-06-16 08:04:24
【问题描述】:
我需要通过 Jest 测试测试我对端点的 POST 请求是否正常工作。我的想法是首先获取我的 Services 表的计数(我正在使用 sequelize orm),然后发送一个新的 post 请求并最终获取新计数并比较旧计数 + 1 是否等于新计数,如果为真,那么 POST 请求就可以正常工作。
test('Create a valid Service', async (done) => {
const service = {
name: "cool",
description: "description"
};
await Service.count().then(async function (count) {
await request(app)
.post('/api/services')
.send(service)
.then(async () => {
await Service.count().then(function (newcount) {
expect(newcount).toBe(count + 1);
});
})
.catch(err => console.log(`Error ${err}`));
});
});
对我来说,测试看起来不错,但是当我运行它时,我得到:
Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.
是否缺少某些内容,或者是否有更好的方法来测试 POST 请求?开玩笑?
【问题讨论】: