How to test the type of a thrown exception in Jest

后端 未结 12 1242
误落风尘
误落风尘 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:06

    From my (albeit limited) exposure to Jest, I have found that expect().toThrow() is suitable if you want to only test an error is thrown of a specific type:

    expect(() => functionUnderTest()).toThrow(TypeError);

    Or an error is thrown with a specific message:

    expect(() => functionUnderTest()).toThrow('Something bad happened!');

    If you try to do both, you will get a false positive. For example, if your code throws RangeError('Something bad happened!'), this test will pass:

    expect(() => functionUnderTest()).toThrow(new TypeError('Something bad happened!'));

    The answer by bodolsog which suggests using a try/catch is close, but rather than expecting true to be false to ensure the expect assertions in the catch are hit, you can instead use expect.assertions(2) at the start of your test where 2 is the number of expected assertions. I feel this more accurately describes the intention of the test.

    A full example of testing the type and message of an error:

    describe('functionUnderTest', () => {
        it('should throw a specific type of error.', () => {
            expect.assertions(2);
    
            try {
                functionUnderTest();
            } catch (error) {
                expect(error).toBeInstanceOf(TypeError);
                expect(error).toHaveProperty('message', 'Something bad happened!');
            }
        });
    });
    

    If functionUnderTest() does not throw an error, the assertions will be be hit, but the expect.assertions(2) will fail and the test will fail.

提交回复
热议问题