【发布时间】:2021-12-08 09:48:43
【问题描述】:
我们确实在我们的代码库as part of defensive programming 中使用console.assert 来检查一些复杂的代码部分并注释关于代码中正在执行的操作/它计算/假设的值等的假设。
例子:
function calculateSomething(a, b, c) {
// assume, we know that the calculation result below should never be negative, because other logic assures that (b - c) < a (This is just an example.)
// or that check is implemented here, but just to make sure you put that negative check before returning the value
const result = a - (b - c);
console.assert(result > 0, 'result must not be negative, but was', result);
return result;
}
console.log('result 1:', calculateSomething(1, 2, 3)); // fails
console.log('result 2:', calculateSomething(1, 3, 2)); // works
现在,我们注意到这只会在生产代码/通常代码运行时在控制台中失败/打印错误消息,但在测试执行代码时明确不会。
如何让console.assert 在测试中也失败?
【问题讨论】:
标签: javascript console jasmine assert