node.js - 如何为 express-session 的 req.session.destroy() 编写简单的 Jest 模拟
问题描述
我正在编写一个具有简单注销突变的 grapqhl 服务器。当我运行服务器时,一切都按预期工作,我可以通过销毁会话并清除 cookie 来注销。
这是解析器:
export default async (root, args, context) => {
console.log("THIS WILL LOG")
await new Promise((res, rej) =>
context.req.session.destroy(err => {
if (err) {
return rej(false);
}
context.res.clearCookie("qid");
return res(true);
})
);
console.log("NEVER HERE BEFORE TIMEOUT");
// 4. Return the message
return {
code: "OK",
message: "You have been logged out.",
success: true,
item: null
};
};
我正在尝试编写一个简单的测试来验证 req.session.destroy 和 res.clearCookie 函数是否被实际调用。在这一点上,我并没有尝试测试是否真的清除了 cookie,因为我实际上并没有启动服务器,我只是在测试 graphql 解析器是否正确运行并且它调用了正确的函数。
这是我测试的一部分:
describe("confirmLoginResolver", () => {
test("throws error if logged in", async () => {
const user = await createTestUser();
const context = makeTestContext(user.id);
context.req.session.destroy = jest
.fn()
.mockImplementation(() => Promise.resolve(true));
context.res.clearCookie = jest.fn();
// this function is just a helper to process my graphql request.
// it does not actually start up the express server
const res = await graphqlTestCall(
LOGOUT_MUTATION, // the graphql mutation stored in a var
null, // no variables needed for mutation
null // a way for me to pass in a userID to mock auth state,
context // Context override, will use above context
);
console.log(res);
expect(context.req.session.destroy).toHaveBeenCalled();
// expect(res.errors.length).toBe(1);
// expect(res.errors).toMatchSnapshot();
});
});
同样,在实际运行服务器时一切正常。问题是当我尝试运行上述测试时,我总是得到一个开玩笑的超时:
Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.
原因是上述解析器的 await 部分将挂起,因为它的 promise.resolve() 永远不会被执行。所以我的控制台将显示“这将记录”,但永远不会出现“超时前永远不会出现”。
我怀疑我需要编写一个更好的笑话模拟来更准确地模拟 context.req.session.destroy 内部的回调,但我无法弄清楚。
有什么想法可以在这里编写更好的模拟实现吗?
context.req.session.destroy = jest
.fn()
.mockImplementation(() => Promise.resolve(true));
不是切割它。想法?
解决方案
尝试
context.req.session.destroy = jest
.fn()
.mockImplementation((fn) => fn(false));
推荐阅读
- powerbi - 行数:(SOME IF CONDITION)+(SOME IF CONDITION) 这是要求 DEX Power BI 计算字段
- c - 我如何创建一个函数来告诉我牌组中最后一张可用的牌是什么?
- java - 为什么我不能将数据插入 MySQL 数据库?
- java - JavaFX JAR 文件不会运行
- algorithm - 为什么不通过代码战中问题平衡数字的所有测试用例?
- c# - 无法上传文件到服务器
- java - JavaFX 按钮设置图像
- sql - 我怎样才能使这个查询不是 CaSeSeNsITIVe?
- javascript - 无法显示来自 MongoDB 的内容,给了我空数组 []
- java - 如何对 jsonObject 的值求和并在 JAVA 的 jsonArray 中创建 sum 的新 jsonobject?