【发布时间】:2022-01-17 07:17:54
【问题描述】:
我有一个用于添加新用户的简单控制器。解析成功(用户添加)后,控制器发送 202 响应。如您所见,该函数使用的是then/catch,而不是使用async/await。
const addUserController = function (req, res, next) {
Users.addOne(req.userid, req.body.email)
.then(() => {
res.status(202).send();
})
.catch((err) => {
console.log(err);
res.status(500).json({ message: "Internal server error." });
});
};
当我在 Jest 中使用 the 测试这个函数时,该函数立即执行,没有转到 then() 部分,导致错误的 200 代码,而不是 202,因此以下测试失败:
it("Should add a user", () => {
let req, res, next, pool;
pool = new Pool();
req = httpsMocks.createRequest();
res = httpsMocks.createResponse();
res.next = null;
req.userid = 1;
req.body = {
id: 2
}
pool.query.mockResolvedValue({rows:[], rowCount: 1});
apiController.addUserController(req, res, next);
expect(res.statusCode).toBe(202);
expect(pool.query).toBeCalledTimes(1);
});
但是,当我这样做时:
it("Should add a user", async () => {
let req, res, next, pool;
pool = new Pool();
req = httpsMocks.createRequest();
res = httpsMocks.createResponse();
res.next = null;
req.userid = 1;
req.body = {
id: 2
}
pool.query.mockResolvedValue({rows:[], rowCount: 1});
await apiController.addUserController(req, res, next);
expect(res.statusCode).toBe(202);
expect(pool.query).toBeCalledTimes(1);
});
那就是我添加了 async/await,它工作正常 - 响应状态码是 202,表示该函数已等待并且测试通过。 但为什么?当我将鼠标悬停在新添加的“等待”VS 代码上时,提示
'await' 对这个表达式的类型没有影响。
这是有道理的——它应该没有效果,因为测试的函数不是异步的,所以它不应该工作,但是它可以工作——只有当我将 async/await 添加到 Jest 函数时它才能正常工作.
谁能给我解释一下?
【问题讨论】:
-
addUserController不返回承诺,因此使用await不会做任何事情(afaik) -
但是在运行 expect() 测试之前,我能做些什么来等待 addUserController 执行?因为显然,在当前阶段,addUserController 只是返回 undefined。我知道我可以将 addUserController 重构为 async/await,然后也使 Jest 中的函数异步/await,但我想了解这里发生了什么以及如何以当前形式解决这个问题
-
httpsMocks是什么,您是否为此使用了库? -
是的,就是这个库:npmjs.com/package/node-mocks-http
标签: javascript node.js testing