我偶然发现了这篇文章:JavaScript的Revealing Module Pattern.我想在我的项目中使用它.
让我们想象一下我有一个函数abc,我在我的主JavaScript文件中调用该函数.
这种模式会使事情变得不同吗?谁能告诉我这种模式的基本例子?
我一直在测试我的 Azure Functions,但无法模拟上下文日志函数。
例如,我有以下 Azure 函数:
module.exports = async function (context, req) {
if (req.query.isGood) {
context.log("Goooood!!!")
context.res = {
body: {
message: "This is good!"
}
};
} else {
context.log.error("Not gooood!!!")
context.res = {
status: 404,
body: {
message: "This is not good!"
}
};
}
}
Run Code Online (Sandbox Code Playgroud)
所以我想检查某个日志发生的次数,例如“log.error”发生一次,“log”发生两次,但我无法模拟这一点。
我尝试了几种组合,例如:
log: {
"": jest.fn(),
"error": jest.fn()
}
Run Code Online (Sandbox Code Playgroud)
此时我对如何模拟这些函数一无所知,并且想知道这是否可能?以及如何创建这些类型的函数?