jestjs - 开玩笑 mockResolvedValueOnce/mockReturnValueOnce 访问函数参数
问题描述
我想编写一个模拟函数,每次调用都会返回不同的值。我遇到的问题是返回值取决于传递给函数的参数值。
//something like this
window.fetch = jest.fn(urlPath)
.mockResolvedValueOnce(fetch({urlPath}))
.mockResolvedValueOnce(fetch({urlPath, arg1: 1}));
解决方案
使用模拟实现。它允许您传递一个函数并以细粒度的方式处理它。
jest.mockImplementation((arg1, arg2) => return whatever(arg1, arg2))
推荐阅读
- javascript - 可以在不使用 Google Maps API 的情况下进行车辆跟踪吗?
- msbuild - msbuild 警告 - MSB3246 & NETSDK1049
- discord - 如何让不和谐的机器人发送邀请?
- express - 我正在尝试通过 heroku 部署 MEAN 应用程序,但我陷入了困境
- javascript - 对话流返回一个空响应,尽管有数据
- kotlin - 错误:Kotlin 中的重载分辨率歧义
- angular - 角度文件上传不适用于 formData
- python-3.x - Selenium webdriver既不打开Chrome也不打开Firefox浏览器,我在pycharm中下载了chromedriver、geckodriver
- javascript - onClick 函数或模式的限制?
- azure - Azure CDN 端点 - 无法添加响应标头