【问题标题】:rejects.toThrow in Jestrejects.toThrow in Jest
【发布时间】:2020-10-12 05:42:20
【问题描述】:

我已经创建了这个类:

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);

【问题讨论】:

标签: javascript node.js typescript jestjs


【解决方案1】:

HostelNotFoundError 不是FunctionalError 类型,它是ErrorList 类的方法,它返回FunctionalError 的新实例。所以在你的单元测试中你必须使用:

rejects.toThrow(FunctionalError);

请注意,您可以使用 toMatch 进行验证,例如错误消息或toMatchObject 来验证错误的属性:

rejects.toMatch('Can\'t find this hostel');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-02
    • 2019-07-15
    • 2022-12-26
    • 2021-11-30
    • 2019-01-20
    • 2019-12-19
    • 2019-01-11
    • 2019-11-16
    相关资源
    最近更新 更多