jestjs - 有没有办法让 `toHaveBeenCalledWith` 匹配正则表达式?
问题描述
我有一个附加随机数的函数,然后调用另一个函数。我想检查它是否被传入的文本调用并匹配任何随机数。我希望能够在没有 Jest 字面上匹配正则表达式的情况下通过正则表达式。就像是:
const typeFn = jest.fn();
function type (text) {
typeFn(text + Math.random());
})
type('hello')
expect(typeFn).toHaveBeenCalledWith(/hello\d+/)
解决方案
您可以使用其中一个辅助函数来expect
代替实际值:
expect(typeFn).toHaveBeenCalledWith(expect.stringMatching(/hello\d+/));
这是一个活生生的例子:https ://repl.it/@marzelin/FrighteningCapitalConferences
推荐阅读
- java - queue.add(...) 返回 true 但没有元素添加到 BlockingQueue
- java - Liferay 7.1 b3 在片段中嵌入 portlet
- angular - Angular 从服务中动态创建和销毁组件
- vba - VBA。根据来自另一个表格的匹配替换表格单元格内容,或者如果未找到匹配则删除整行
- android - 实现了哪种数据结构来管理手机中的文件和文件夹?
- angular5 - import Routes [ts] 'types' 只能在 .ts 文件中使用。虽然它们已经在 app.module.ts 中
- javascript - Xamarin Android WebView - JDE E1 Web 门户
- python - NaN 设置为 -1 时缩放特征的效果
- arrays - 如何显示数组的元素?
- node.js - 获取 PNG 图像时出现意外的缓冲区结果