unit-testing - 笑话:模拟高阶函数
问题描述
我正在尝试模拟第三方库以使用 Jest 编写集成测试
产品。代码部分:
const processedContent = await remark().use(html).process(content);
所以我想将备注模拟为一个 HOF 来返回一个函数(使用)来返回另一个函数(进程)
我的做法:
remark.mockImplementationOnce(() => {
return function use() {
return function process() {
return testMessageContent;
};
};
})
因此,如果 console.log(remark) 我可以看到它如何返回一个函数,但是当我尝试 console.log(remark().use() 时,我得到:
TypeError: (0 , _remark.default)(...).use is not a function
72 | //const ans = await addReply(content, comment, userInfo._id);
73 |
> 74 | console.log(remark().use());
如果我:
const use = remark()
const process = use()
const message = process()
一切正常。我不明白为什么这不起作用。任何帮助将不胜感激!
提前致谢!
解决方案
模拟不会返回带有use
方法的对象,并且调用的命名函数use
不会改变它的工作方式。
它应该是:
remark.mockImplementationOnce(() => {
return { use() {
return { process() {
return testMessageContent;
} };
} };
})
推荐阅读
- javascript - html2canvas is not reading inline background-color style
- delphi - 在内联汇编中将常量读入 SSE/AVX 寄存器
- sql - Subtract all oracle tables with prefix USER from tables with prefix ALL in single statement
- git - Are predefined variables in Gitlab CI/CD avaliable in .gitlab-ci.yml?
- javascript - preventDefault undefined in scroll offset script
- algolia - How to concatenate results on algolia
- windows - 有没有办法在 Windows 10 上触发 Vala MSYS2 的弹出通知?
- react-native - 在本机反应中发送安全的 REST API 请求
- android - 如何检查引导加载程序是否已解锁
- android - 应用程序截图上传到 Google Play 商店后变得模糊