【发布时间】:2021-07-06 05:20:24
【问题描述】:
我无法实现 100% 的 Jest 线路覆盖率。 Jest 在分支语句中有 return 语句的行上显示未覆盖的行。
export class Galactic {
constructor(earthAge) {
this.earthAge = earthAge;
this.mercuryAge = 0; //.24 earth years
this.venusAge = 0; //.62
this.marsAge = 0; //1.88
this.jupiterAge = 0; //11.86
this.averageLife = 65;
}
lifeExpOnMars() {
if (this.earthAge > this.averageLife) {
let surpassedYears = (this.earthAge - this.averageLife) * 1.88;
return Math.floor(surpassedYears);
} else {
let remainingYears = this.averageLife - this.earthAge;
return Math.floor(remainingYears * 1.88);
}
}
}
我在函数中的第一个 return 语句没有显示 Jest 覆盖,因为该代码没有运行,因为我通过第一个 if 语句传递的输入不正确。第二个 return 语句显示为已覆盖,因为这是实际返回的代码,因为第一个 if 语句基于我的输入 earthAge 小于 averageLife 为 false。
我需要帮助来确定我可以编写一个涵盖第一个 return 语句的测试,因为它不需要执行,因为条件不满足。
参考测试:
describe('Galactic', function(){
let galacticAge;
beforeEach(function(){
galacticAge = new Galactic(25)
test('return life expectancy on Mars', function(){
expect(galacticAge.lifeExpOnMars()).toEqual(75);
});
【问题讨论】:
标签: javascript testing jestjs tdd