我正在使用 Express 和 Express-JWT。
在 Express() 实例中我使用:
const api = express()
api.use(jwt({
// my options
}))
为了在测试中模拟这一点,我使用 mocks\express-jwt\index.js
文件,其中包含:
const jwt = jest.fn().mockImplementation(options => (req, res, next) => {
// set dummy req.user and call
next()
})
module exports = jwt
这一切都运行良好。 现在我想跳过根端点的 JWT,因此我将 jwt 用法更改为:
api.use(jwt({
// my options
}).unless({path:['/']}))
在我的模拟文件中我添加了:
jwt.unless = jest.fn().mockImplementation(options => (req, res, next) => {
next()
})
但是,测试现在总是失败,function except is not Defined
。
有人知道如何模拟这个除非
行为吗?
最佳答案
除非
被用作调用jwt
结果的属性。
因此,要模拟它,请将 unless
模拟添加为 jwt
模拟返回的函数的属性:
const jwt = jest.fn().mockImplementation(options => {
const func = (req, res, next) => {
// set dummy req.user and call
next();
};
func.unless = jest.fn().mockImplementation(options => (req, res, next) => {
next();
});
return func;
});
module.exports = jwt;
关于node.js - 如何模拟 Express JWT except 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55517098/