如何对Jest中的错误消息进行单元测试

时间:2020-06-15 18:23:08

标签: typescript jestjs ts-jest

我能够测试某个功能是否在开玩笑地抛出错误。

function calculateArea(input: any): number {
    if (input === undefined || typeof input !== 'number') {
        throw new Error('Invalid input : ' + input);
    } 
   return input * input;
}

test('tests invalid input', () => {
     let input:string = '45';
     expect(() => {
        calculateArea(input);
     }).toThrow('Invalid input : ' + input);
});

现在,即使没有错误消息,单元测试也可以正常工作

test('tests invalid input', () => {
     let input:string = '45';
     expect(() => {
        calculateArea(input);
     }).toThrow();
});

我如何也可以测试错误消息?

0 个答案:

没有答案