| typeof input !== 'number') { throw new ...

问题描述 投票:0回答:0

我能够测试某个函数是在开玩笑地抛出一个错误。

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

我如何也能测试错误信息?

typescript jest ts-jest
© www.soinside.com 2019 - 2024. All rights reserved.