How to test the type of a thrown exception in Jest

后端 未结 12 1221
误落风尘
误落风尘 2020-12-04 18:37

I\'m working with some code where I need to test the type of an exception thrown by a function (is it TypeError, ReferenceError, etc.?).

My current testing framework

12条回答
  •  暖寄归人
    2020-12-04 19:03

    Jest has a method, toThrow(error), to test that a function throws when it is called.

    So, in your case you should call it so:

    expect(t).toThrowError(TypeError);
    

    The documentation.

提交回复
热议问题