【发布时间】:2021-03-09 19:40:15
【问题描述】:
我正在尝试模拟 express-jwt 的 .unless 函数,目前有这个模拟设置来比较 JWT 与管理员和非管理员。在我们添加 .unless 之前,这一直是完美的,我不确定解决问题的最佳方法。
jest.mock("express-jwt", () => {
return options => {
return (req, res, next) => {
receivedToken = req.headers.authorization.substring(7);
req.user = { preferred_username: "tester" };
next();
};
};
});
那是模拟。我尝试将其移入 const 并添加:mockFun.unless = jest.fn() 但无济于事。
谢谢
编辑:
我尝试过的事情之一如下:
jest.mock("express-jwt", () => {
const mockFunc = () => {
return (req, res, next) => {
receivedToken = req.headers.authorization.substring(7);
req.user = { preferred_username: "tester" };
next();
};
};
mockFunc.unless = jest.fn();
return mockFunc
});
【问题讨论】:
-
我尝试将其移入 const - 你到底做了什么?
-
@EstusFlask 进行了编辑以反映这一点
标签: javascript node.js unit-testing jestjs mocking