【发布时间】:2020-11-14 05:34:16
【问题描述】:
我有一个简单的函数,如果输入低于 0 就会抛出错误:
export const hoursMinutesSecondsFromSeconds = (inputSeconds) => {
if (inputSeconds < 0) {
throw new Error('illegal inputSeconds < 0');
}
let rem = Math.abs(inputSeconds);
let divisor = 3600;
const result = [];
while (divisor >= 1) {
result.push(Math.floor(rem / divisor));
rem = rem % divisor;
divisor = divisor / 60;
}
return result;
};
我正在尝试使用低于 0 的输入来测试此函数,如下所示:
import { hoursMinutesSecondsFromSeconds } from './timetools';
describe('hoursMinutesSecondsFromSeconds', () => {
it('throws error', () => {
expect(hoursMinutesSecondsFromSeconds(-2)).toThrowError('illegal inputSeconds < 0');
});
});
但是,当我运行这个测试时,测试失败并且我收到一条错误消息:
Error: illegal inputSeconds < 0
当它抛出一个完全像我期望它在我的测试中抛出的错误时,为什么它没有通过测试?
【问题讨论】:
标签: javascript jestjs