javascript - rejects.toThrow in Jest
问题描述
我创建了这个类:
export class ErrorList {
public HostelNotFoundError(details: ErrorDetail): FunctionalError {
return new FunctionalError('1', 'Can\'t find this hostel', details);
}
并在服务中:
throw new ErrorList().HostelNotFoundError({} });
我想知道在 Jest 中是否可以执行以下操作:
rejects.toThrow(HostelNotFoundError);
解决方案
HostelNotFoundError
不是类型FunctionalError
,它是类的方法,ErrorList
它返回 的新实例FunctionalError
。所以在你的单元测试中你必须使用:
rejects.toThrow(FunctionalError);
请注意,您可以使用toMatch
来验证错误消息或toMatchObject
验证错误的属性:
rejects.toMatch('Can\'t find this hostel');
推荐阅读
- java - @ConfigurationProperties 如何在 Spring 内部工作
- laravel - 动态创建 env 文件 laravel
- compilation - Pro*C 程序在 AIX 中正常运行,但在 RHEL 中无法正常运行
- python - 将 Enum 转换为 List 时跳过元素
- postman - 邮递员根据环境值发送请求
- allennlp - 使用“Predictor.from_path”加载“decomposable-attention-elmo”时出现问题
- r - R dplyr:按组过滤常用值
- sql - 选择满足动态表列出的要求的条目
- python - 为最短路径赋予与绘图中其他路径不同的颜色
- tableau-api - 在 Tableau 中创建包含 Excel 工作表列的下拉菜单